How to Get First and Last Word of String in Python?
Hello Folks,
In this comprehensive tutorial, we will learn python get first and last word of string. This post will give you a simple example of how to get first and last word of string in python. This article goes in detailed on get first and last word of string python. I explained simply step by step print first and last word of string in python. So, let us see in detail an example.
In this examples, i will add myString variable with hello string. Then we will use key to getting first and last 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 1: Python Get First Word from String
main.py
# Declare String Variable myString = "Hello This is ItSolutionStuff.com" # Getting First Word from String firstWord = myString.split()[0] print(firstWord)
Output:
Hello
Example 2: Python Get Last Word from String
main.py
# Declare String Variable myString = "Hello This is ItSolutionStuff.com" # Getting Last Word from String firstWord = myString.split()[-1] print(firstWord)
Output:
ItSolutionStuff.com
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Python Remove Last Character from String Example
- Python Remove First Character from String Example
- How to Get Last Character from String in Python?
- How to Get First Character from String in Python?
- How to Remove Last Word from String in Python?
- How to Remove First Word from String in Python?
- How to Get Last Word from String in Python?
- How to Get First Word from String in Python?
- Python String Convert to Lowercase Example
- Python String Convert to Uppercase Example
- Python Convert List into String with Commas Example
- How to Convert List into String in Python?