• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Get First Word in String

Python Get First Word in String

February 12, 2022 Leave a Comment

In Python, we can easily get the first word in a string. To do so, we can use the Python split() function and then access the first element of the list of words.

string = "This is a string with words."

first_word = string.split(" ")[0]

#Output:
This

When working with strings in our Python code, it can be useful to be able to extract specific words from the string variables.

In particular, it would be useful to be able to get the first word from a string of words.

With Python, we can easily get the first word from a string.

The easiest way to get the first word from a string in Python is to use the Python split() function, and then access the first element (position “0”).

Below shows an example of how to get the first word in a string using Python.

string = "This is a string with words."

first_word = string.split(" ")[0]

#Output:
This

How to Get the Last Word of a String

In a very similar way to above, we can also get the last word of a string.

The difference between getting the first and last word in a string is that now we will be getting the last element after splitting the original string.

To get the last word from a string, we use the Python split() function and then access the “-1” position element.

Below is an example of how you can get the last word in a string using Python.

string = "This is a string with words."

last_word = string.split(" ")[-1]

#Output:
words.

Hopefully this article has been useful for you to understand how to get the first word of a string in Python.

Other Articles You'll Also Like:

  • 1.  Log Base 10 Python – Find Logarithm of Number with Base 10 with log10()
  • 2.  Using Python to Check If List of Words in String
  • 3.  How to Create Block and Multi-Line Comments in Python
  • 4.  Remove Element from Set in Python
  • 5.  Convert First Letter of String to Lowercase in Python
  • 6.  Remove Empty Lists from List in Python
  • 7.  Changing Python Turtle Speed with speed() Function
  • 8.  Get Last Day of Month Using Python
  • 9.  Get Days in Month from Date in pandas DataFrame
  • 10.  Check if Class is Subclass in Python with issubclass()

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy