python-string-method

A Beginner Journey Of Python String Method

In This Article You Will Learn About Python String Method strip(), maketrans(), partition(), find(), replace(), index()

Python String Method – Before moving ahead, let’s know a little bit about Isspace(), Istitle(), Join(), Just(), Lower()

strip() method – It returns copy of original string after removing unnecessary characters (space is the default leading character to remove).

Syntax - string.strip( characters )

Parameter Values -

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

Example 1- It removes spaces to the left of the string.

x = '    Python'
z = x.strip()

print('I like', z , 'language')
Python String Method
As it is shown clearly that string returned after removing whitespace.

Example 2- Using strip() method to remove unnecessary punctuation.  

x = ",,,,,httyuhd.....python"
z = x.strip(' , ')

print(z)
Python String Method
As it is shown clearly that string returned after removing unnecessary punctuation.

maketrans() method – It returns a mapping table for translation to be used in translations.

Syntax - string.maketrans(x[, y[, z]]). Variable y and z are optional arguments.

x - It is the first argument and passed it in dictionary.

y - It is the second argument and passed it in string.

z - It is the third argument and passed it as mapping to none.

Example 1- Using maketrans() method passing argument as dictionary.

x = {"p": "972", "q": "871", "r": "463"}
z = "pqr"

print(z.maketrans(x))
Python String Method
As it is shown clearly that, a dictionary (x) is defined. It contains a mapping of characters p, q and r to 972, 871 and 463 respectively.

Example 2- Using maketrans() method passing argument as string.

x = 'pqr'
y = "def"
z = 'pqr'

print(z.maketrans(x, y))
Python String Method

Example 3- Using maketrans() method passing argument as dictionary.

x = "pqr"
y = "jes"
z = "pqr"

print(z.maketrans(x, y))
Python String Method
As it is shown clearly that it returned value of string in mapping table.

partition() method – It searched specified string, and splits the string in three character containing a tuple.

First part - It returns the part of before spilt. In other words, from starting to till the point spilt.

Second part - It returns the spilt part or specified string.

Third part - It returns the part of after spilt. In other words, from the point after the spilt to till last.
Syntax - string.partition( value )

Parameter Values -

value - It is required argument. It searches for string.

Note: – This method search for the first occurrence of the specified string.

Example 1- Use of partition() method to search the specified string or word and return three elements containing tuple.

x = 'python is a programming language'
z = 'programming'

print(x.partition(z))
Python String Method
As it is shown clearly that string is split in three character containing a tuple.

Example 2- Check If specified value not found in string.

x = 'python is a programming language'
z = 'computer'

print(x.partition(z))
Python String Method
As it is shown clearly that first it returned whole string with two empty string at the last position. It replaced not found string with two empty string.

replace() method – It returns replaces version of old string into new string. In other words, it replaces old specified string into new specified string.

Syntax - string.replace(old string, new string, count)

Parameter Values -

old string - It is required argument. The string to be changed into new string.

new string - It is required argument. The string which changed old string.

count - It is optional argument. It is specified a number of occurrences of the old value to be replaced. As default it counts all occurrence.

Example 1- Using replace() method to replace old string in the new string.

x = 'python is a programming language.'
print(x.replace('programming', 'computer'))
Python String Method
As it is shown clearly that old string replaced with new string.

Example 2- It replaces all occurrence of the word ‘Python’

x = 'Python is a Python language.'
print(x.replace('Python', 'computer'))
Python String Method
As it is shown clearly that it replaces all occurrence of specified string.

Example 3- It replaces old string in the new string between specified range.

x = 'Python is a Python language.'
print(x.replace('Python', 'computer', 1))
Python String Method
As it is shown clearly that it replaced old string with new string between the range.

rfind() method – It returns the highest index number of first most last occurrence specified substring. If value not found it returns -1.

Note: rfind() and rindex() method is almost same but difference is that, rfind() returns returns -1 if value not found but rindex() raised an error.

Syntax - string.rfind( value, start, end )

Parameter Values -

value - It is required argument to search for.

start - It is optional argument for at what position search is starts. If no position to start, default is 0.

end - It is optional argument. for at what position search is ends. Default is to the end of the string.

Example 1- Check at what last position letter “a” is appeared in given string.

x = 'python is a language.'
z = x.rfind('a')

print(z)
Python String Method
As it is shown clearly that it returned the index number of first last specified string.

Example 2- Check at what last position letter “a” is appeared between the range in given string.

x = 'python is a language.'
z = x.rfind('a', 7, 21)

print(z)
Python String Method
As it is shown clearly that it returned the index number of first last specified string between the range.

Example 3- Check if value is not found, it returns -1. But the rindex() method will raise an exception:

x = 'python is a language.'
z = x.rfind('k')
y = x.rindex('k')

print(z)
print(y)
Python String Method
As it is shown clearly that index() method returned an error because it did not get string ‘k’.

rindex() method – It returns the index number of specified value of last occurrence.

Syntax - string.rindex(value, start, end)

Parameter Values -

value - It is required argument. It is specified value search for.

start - It is optional argument. At what position start for.

end - It is optional argument. At what position end for.

Example 1- Getting index number of specified value (‘o’) of first last occurrence.

x = 'Hello, python world'
z = x.rindex('o')

print(z)
Python String Method
As it is shown clearly that It returns index number of specified value (‘o’) of first last occurrence.

Example 2- It returns the index number of specified value of first last occurrence between range in given string.

x = 'Hello, python world'
z = x.rindex('o', 5, 18)

print(z)
Python String Method
As it is shown clearly that It returned the index number of specified values of first last string between the range.

Note:  rindex() and rfind() method is almost same but difference is that, rindex() raised an error if value not found whereas rfind() returns -1.

Example 3- Check if value is not found, it returns an error. But the rfind() method returned -1.

x = 'python is a language.'
z = x.rindex('k')
y = x.rfind('k')

print(z)
print(y)
Python String Method
As it is clearly that it returned an error because index() method did not get string ‘k’.

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