codingstreets
Search
Close this search box.
python-string-methods-rsplit-rstrip-split-splitlines-startswith

Python String Methods rsplit(), rstrip(), split(), splitlines(), startswith()

In this article, learn essential Python string methods like rsplit(), rstrip(), split(), splitlines(), and startswith(). Master the art of manipulating strings, splitting them into substrings, removing trailing whitespace, checking prefixes, and more. This comprehensive tutorial will sharpen your Python string-handling skills and empower you to write clean and efficient code.

Python String Methods – Before moving ahead, let’s see Python String Methods.

Table of Contents

rsplit() method

rsplit() split the string from the right position; separated by value and returns in the list. If no max is specified, it returns the same split version of the string.

Syntax – string.rsplit( separator, maxsplit )

Parameter Values –

separator – Optional. Specifies the value to separate the string, if not found; default is taken as whitespace.

max split – Optional. Specifies how many maximum splits to do. If not found, default returns -1, which means all occurrences.

Example: Default separate by whitespace.

				
					text = 'Python is a programming language'
r_split = text.rsplit(" ")

print(r_split)

#output:
['Python', 'is', 'a', 'programming', 'language']
				
			

Example: Check if a separate is found.

				
					text = 'Python: is: a: programming: language:'
r_split = text.rsplit(":")

print(r_split)

#output:
['Python', ' is', ' a', ' programming', ' language', '']
				
			

Example: Check if a separate is found.

				
					text = 'Python: is: a: programming: language:'
r_split = text.rsplit(",")

print(r_split)

#output:
['Python: is: a: programming: language:']
				
			

Example: Separate the string in the maximum number of separators.

				
					text = 'Python: is: a: programming: language:'
r_split = text.rsplit(":",3)

print(r_split)

#output:
['Python: is: a', ' programming', ' language', '']
				
			

Example: Default max split returns string at all occurrences.

				
					text = 'Python: is: a: programming: language:'
r_split = text.rsplit(":",)

print(r_split)

#output:
['Python', ' is', ' a', ' programming', ' language', '']
				
			

Example:

				
					text = 'Python: is: a: programming: language:'
r_split = text.rsplit(":",0)

print(r_split)

#output:
['Python: is: a: programming: language:']
				
			

rstrip() method

rstrip() removes the specified characters; started at right and returns the string. 

Syntax – string.rstrip( characters )

Parameter Values –

characters – Optional. A set of characters to be removed.

Example: Returns the split version of the string.

				
					text = 'Python is a programming language ....      .....'
r_strip = text.rstrip('... ')

print(r_strip)

#output:
Python is a programming language  
				
			

Example: Returns the split version of the string.

				
					text = 'Python is a programming language ....      .....'
r_strip = text.rstrip('...')

print(r_strip)

#output:
Python is a programming language ....      
				
			

Explanation: From the right side separator removed only (…). After that whitespace is mentioned in the string but not in the separator; therefore rest of the string returned the same.

				
					text = 'Python is a programming language,,,'
r_strip = text.rstrip(',')

print(r_strip)

#ouput:
Python is a programming language
				
			

split() method

Returns the split version of the string. If no separator is defined, the default is whitespace.

Syntax – string.split( separator, max split)

Parameter Values –

separator – Optional. Specifies the value to separate the string, if not found; default is taken as whitespace.

max split – Optional. Specifies how many maximum splits to do. If not found, default returns -1, which means all occurrences.

Example: Use default separator whitespace.

				
					text = 'Python: is: a: programming: language:'
split = text.split(" ")

print(split)

#output:
['Python:', 'is:', 'a:', 'programming:', 'language:']
				
			

Example: Check if separate is found.

				
					text = 'Python: is: a: programming: language:'
split = text.split(":")

print(split)

#ouput:
['Python', ' is', ' a', ' programming', ' language', '']
				
			

Example: Check if separate is found.

				
					text = 'Python*is*a*programming*language'
split = text.split(":")

print(split)

#output:
['Python*is*a*programming*language']
				
			

Example: Separate string at the specified max split.

				
					text = 'Python*is*a*programming*language'
split = text.split("*", 3)

print(split)

#output:
['Python', 'is', 'a', 'programming*language']
				
			

splitlines() method

Syntax – string.splitlines( keep line breaks)

Parameter Values – 

Keep line breaks – Optional. True if line breaks are included, otherwise False.

Example: split lines in the list at the breaker.

				
					text = 'python in \n Python'
split = text.splitlines()

print(split)

#output:
['python in ', ' Python']
				
			

Example: Split the string, but keep the line breaks.

				
					text = 'python in \n Python'
split = text.splitlines(True)

print(split)

#output:
['python in \n', ' Python']
				
			

Example: Split the string, but keep the line breaks.

				
					text = 'python in \r Python'
split = text.splitlines()

print(split)

#output:
['python in ', ' Python']
				
			

Following line boundaries of splitlines – 

Name

Description

\n

New line

\r

Carriage Return

\r\n

Carriage Return + Line Feed

\v or \x0b

Line Tabulation

\f or \x0c

Form Feed

\x1c

File Separator

\x1d

Group Separator

\x1e

Record Separator

\x85

New Line (C1 Control Code)

\u2028

Line Separator

\u2029

Paragraph Separator

startswith() method

startswith() returns True, if string starts with the specified value, otherwise False.

Syntax – string.startswith( value, start, end )

Parameter Values –

value – Required argument. It checks if the specified value starts with.

start – Optional. An Integer specifying at which position to start the search.

end – Optional. An Integer specifying at which position to end the search.

Example: Check if the string starts with the specified value.

				
					text = 'Hello, Python!'
starts_with = text.startswith('H')

print(starts_with)

#output:
True
				
			

Example: Check if the string starts with the specified value.

				
					text = 'Hello, Python!'
starts_with = text.startswith('Hello')

print(starts_with)

#output:
True
				
			

Example: Check if the string starts with the specified value.

				
					text = 'Hello, Python!'
starts_with = text.startswith('h')

print(starts_with)

#output:
False
				
			

Example: Check if the string starts with the specified value within a range.

				
					text = 'Hello, Python!'
starts_with = text.startswith('P', 7, 10)

print(starts_with)

#output:
True
				
			

Conclusion

Python string methods rsplit(), rstrip(), split(), splitlines(), and startswith() provides a comprehensive overview of these powerful tools for manipulating and analyzing strings in Python.

The article delves into each method, explaining its purpose and functionality. Readers are guided through practical examples and use cases, allowing them to grasp how these methods can be applied in real-world scenarios. The article emphasizes the importance of understanding string manipulation and the role it plays in various programming tasks.

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on