How To Split A String And Get The Last Element In Python

Split a string and get the last element in Python

To split a string and get the last element in Python, you can use one of the four built-in functions in Python. I’ll go out below to get an idea for this topic. Please read the article in detail. 

Split a string and get the last element in Python

Using the str.split() function

The first way to split a string and get the last element in Python is using the str.split() function. Look at the syntax and example below to understand more about it.

Syntax:

str.split()

Example:

  • Initialize a string containing delimiters.
  • Split the string using the str.split() function and get the last element of the string.
stringObj = 'visit#learnshareit#website'

# Use the str.split() function to split the string and get the last element in the string.
lastElement = stringObj.split('#')[-1]
print('The last element of the string:', lastElement)

Output:

The last element of the string: website

Using the re.split() function

The second way is using the re.split() function. Its syntax and example is below.

Syntax:

re.split(RegEx, string, maxsplit)

Parameters:

  • RegEx: regular expressions.
  • string: string you want to compare.
  • maxsplit: is the maximum number of splits. If not specified, Python defaults to an infinite number of splits.

Example:

  • Import the re module.
  • Initialize a string containing delimiters.
  • Split the string using the re.split() function and get the last element of the string.
import re

stringObj = 'visit#learnshareit#website'

# Use the re.split() function to split the string and get the last element in the string.
lastElement = re.split('#', stringObj)[-1]
print('The last element of the string:', lastElement)

Output:

The last element of the string: website

Using the str.rsplit() function

Syntax:

str.rsplit(separator, maxsplit)

Parameters:

  • separator: the value you want to round.
  • maxsplit: number of digits after the decimal point. The default is 0.

Example:

  • Initialize a string containing delimiters.
  • Split the string using the str.rsplit() function and get the last element of the string.
stringObj = 'visit#learnshareit#website'

# Use the str.rsplit() function to split the string and get the last element in the string.
lastElement = stringObj.rsplit('#')[-1]
print('The last element of the string:', lastElement)

Output:

The last element of the string: website

Using the str.rpartition() function

Using the str.rpartition() function also is a great solution to split a string and get the last element.

Syntax:

string.rpartition(value)

Parameters:

  • value: The string you want to search for. Required parameter.

The rpartition() function returns a string and splits the string into three parts before the specified string, the specified string, and after the specified string.

Example:

  • Initialize a string containing delimiters.
  • Split the string using the str.rpartition() function and get the last element of the string.
stringObj = 'visit#learnshareit#website'

# Use the str.rpartition() function to split the string and get the last element in the string.
lastElement = stringObj.rpartition('#')[-1]
print('The last element of the string:', lastElement)

Output:

The last element of the string: website

Summary

Those are four ways to split a string and get the last element in Python. You can use one of them. They are all built-in functions in Python. If there is a better way, please comment below. We appreciate it.

Maybe you are interested:

Leave a Reply

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