python-string-method

A Beginner Journey Of Python String Method

In This Article You Will Learn About Python String Method just(), spilt(), strip(), splitlines()

Python String Method – Before moving ahead, let’s know a little bit about Strip(), Maketrans(), Partition(), Find(), Replace(), Index()

rjust() method – It fills a string with left side from specified value.

Syntax - string.rjust( length, character)

Parameter Values -

length - It is required argument. It returns the length of the given string. 

character -  It is an optional argument. It takes character to the left of the string to fill the missing space. Default is taken as " " (space).

Example 1- It returns 30-character long string right justified versions of the word ‘python’. Default is taken as ” ” (space).

x = ‘python is a computer language.'
z = x.rjust(30)

print(z)
python-string-method

Example 2- Use of rjust() method for padding as the letter ‘q’-

x = 'python'
z = x.rjust(10, 'q')

print(z)
python-string-method
As it is shown clearly that it filled the rest of string value with the given string.

rspilt() method – It returns splits version of an original string into a list, started at the right position.  If no “max” is specified, this method will return the same as the split() method.

Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Syntax - string.rsplit( separator, maxsplit )

Parameter Values -

separator - It is optional argument. It specifies separator to spilt the string. If no separator then default is whitespace is a separator.

maxsplit - It is optional argument. It specifies number of spilt to do. If no maxspilt then by default value is -1, which is "all occurrences".

Example 1- Using rsplit() method to split string into a list, with comma (,) as separator-

x = 'python, is, a, programming, language,'
z = x.rsplit(",")

print(z)
python-string-method
As it is shown clearly that it returned a split version of string in the list.

Example 2- Using rsplit() method to split the string into a list with maximum of 2 items.

x = 'python, is a programming language'
z = x.rsplit(" , " , 2)

print(z)
python-string-method
As it is shown clearly that it returned string after splitting into 2 maximum characters.

rstrip() method – It returns copy of string after removing unnecessary string characters based on the string argument passed.

Syntax - string.rstrip( characters )

Parameter Values -

characters - It is optional argument. A set of characters to be removed.

Note: If the chars argument is not provided, all whitespaces on the right are removed from the string.

Example 1- Using rstrip() method to remove spaces at the right position of the given string.

x = 'python is a programming language. '
z = x.rstrip()

print(z)
python-string-method

Example 2- Use of rstrip() method to remove unnecessary characters.

x = "programming,,,,yhhhtt....."
z = x.rstrip(" , .yht")

print(z)
python-string-method
As it is shown clearly that unnecessary string is removed from original string.

split() method – It returns a list of spilt string. Separator can be taken otherwise default will be taken whitespace.

Note: If string contains maxsplit, then list will contain the specified number of elements plus one.

Syntax - string.split( separator, maxsplit )

Parameter Values -

separator - It is optional argument. It specified separator to be used in spilt of string. If no separator is given, then whitespace will be taken as default separator.

maxsplit - It is optional argument. It specified number of spilt to do. If no separator is given, then -1 will be taken as default value, which is "all occurrences"

Example 1- Using split() method to split the string into a list.

x = 'python is a programming language.'
z = x.rsplit()

print(z)
python-string-method
As it is shown clearly that it retuned a split version of string.

Example 2- Using split() method to use a character as a separator.

x = 'python*in*python'
z = x.split('*')

print(z)
python-string-method

splitlines() method – It returns a version of split line into a list.

Syntax - string.splitlines( keep line breaks)

Parameter Values - 

Keep line breaks - It is optional argument. It is True if line breaks are included otherwise False.

Example 1- Use of splitlines() to break line into a list.

x = 'python in \n Python'
z = x.splitlines() 

print(z)
python-string-method

Example 2- Use of splitlines() to split the string, but keep the line breaks.

x = 'python in \n Python'
z = x.splitlines(True)

print(z)
python-string-method

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on