Get First Word from String in Python Example

25-Feb-2023

.

Admin

Get First Word from String in Python Example

Hi Dev,

In this short tutorial we will cover an get first word from string in python example. We will look at example of python first word of string. it's simple example of how to get first word in python. you can see how to get first word from string in python.

In this example, i will add myString variable with hello string. Then we will use split() function to getting first word from string in python. So, without further ado, let's see simple examples: You can use these examples with python3 (Python 3) version.

Example :


main.py

# Declare String Variable

myString = "Hello This is nicesnippets.com"

# Getting First Word from String

firstWord = myString.split()[0]

print(firstWord)

Output:

Hello

#Python