Just split the string on whitespace, and get the last element of the array. Or use rsplit[]
to start splitting from end:
>>> st = 'Hello my name is John'
>>> st.rsplit[' ', 1]
['Hello my name is', 'John']
>>>
>>> st.rsplit[' ', 1][1]
'John'
The 2nd argument specifies the number of split
to do. Since you just want last element, we just need to split once.
As specified in comments, you can just pass None
as 1st argument, in which case the default delimiter which is whitespace will be used:
>>> st.rsplit[None, 1][-1]
'John'
Using -1
as index is safe, in case there is no whitespace
in your string.
Contents
- Introduction
- Example 1: Split String by Space
- Example 2: Split String by One or More Adjacent Spaces
- Example 3: Split String by Any White Space Character
- Summary
You can split a string with space as delimiter in Python using String.split[] method.
In this tutorial, we will learn how to split a string by a space character, and whitespace characters in general, in Python using String.split[] and re.split[] methods.
Refer Python Split String to know the syntax and basic usage of String.split[] method.
Example 1: Split String by Space
In this example, we will take a string which contains words/items/chunks separated by space character. We shall then split the string by space using String.split[] method. split[] method returns list of chunks.
Python Program
str = '63 41 92 81 69 70'
#split string by single space
chunks = str.split[' ']
print[chunks]
Run
Output
['63', '41', '92', '81', '69', '70']
Example 2: Split String by One or More Adjacent Spaces
In this example, we will take a string with chunks separated by one or more single space characters. Then we shall split the string using re.split[] function. re.split[] returns chunks in a list.
We shall use re
python package in the following program. re.split[regular_expression, string]
returns list of chunks split from string
based on the regular_expression
.
Python Program
import re
str = '63 41 92 81 69 70'
#split string by single space
chunks = re.split[' +', str]
print[chunks]
Run
Regular Expression +
represents one or more immediately occuring spaces. So, one or more single space characters is considered as a delimiter.
Output
['63', '41', '92', '81', '69', '70']
One ore more adjacent spaces are considered as a single delimiter because of the regular expression.
Example 3: Split String by Any White Space Character
In this example, we shall split the string into chunks with any white space character as delimiter.
Following are the list of white space characters from ASCII Table.
ASCII Hex Code | Description |
09 | horizontal tab |
0A | New line feed |
0B | Vertical Tab |
0D | Carriage Return/ Form Feed |
20 | Space |
By default, String.split[], with no argument passed, splits the string into chunks with all the white space characters as delimiters.
Python Program
import re
str = '63 41\t92\n81\r69 70'
#split string by single space
chunks = str.split[]
print[chunks]
Run
Output
['63', '41', '92', '81', '69', '70']
Summary
In this tutorial of Python Examples, we learned how to split a string by space using String.split[] and re.split[] methods. Also, we learned how to split a string by considering all whitespace characters as delimiter.
Related Tutorials
- Python Split String into Specific Length Chunks
- Python Split String by Comma
- How to Split String by Underscore in Python?
- Python Split String into List of Characters
- Python Split String by New Line
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Sometimes, more than finding a substring, we might need to get the string that is occurring before the substring has been found. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using partition[]
The partition function can be used to perform this task in which we just return the part of the partition occurring before the partition word.
Python3
test_string
=
"GeeksforGeeks is best for geeks"
spl_word
=
'best'
print
[
"The original string : "
+
str
[test_string]]
print
[
"The split string : "
+
str
[spl_word]]
res
=
test_string.partition[spl_word][
0
]
print
[
"String before the substring occurrence : "
+
res]
Output :
The original string : GeeksforGeeks is best for geeks The split string : best String before the substring occurrence : GeeksforGeeks is
Method #2 : Using split[]
The split function can also be applied to perform this particular task, in this function, we use the power of limiting the split and then print the former string.
Python3
test_string
=
"GeeksforGeeks is best for geeks"
spl_word
=
'best'
print
[
"The original string : "
+
str
[test_string]]
print
[
"The split string : "
+
str
[spl_word]]
res
=
test_string.split[spl_word][
0
]
print
[
"String before the substring occurrence : "
+
res]
Output :
The original string : GeeksforGeeks is best for geeks The split string : best String before the substring occurrence : GeeksforGeeks is
Method #3 : Using find[]
Python3
test_string
=
"GeeksforGeeks is best for geeks"
spl_word
=
'best'
print
[
"The original string : "
+
str
[test_string]]
x
=
test_string.find[spl_word]
res
=
test_string[
0
:x]
print
[
"String before the substring occurrence : "
+
res]
Output
The original string : GeeksforGeeks is best for geeks String before the substring occurrence : GeeksforGeeks is
The Time and Space Complexity for all the methods are the same:
Time Complexity: O[n]
Space Complexity: O[n]