String Operations in Python Part 2

In the previous article we saw various string operations like concatenation, and use of index( ) and rindex( ) functions. Now we will look at more String Operations in Python.

Utilizing String Operations in Python

len( ): It returns the number of characters in the string.

s1="geeks"
print(len(s1))
Output:
5

upper( ): It converts the lowercase characters in the given string to uppercase.

s1="geeks"
s2=s1.upper()
print(s2)
Output:
GEEKS

lower( ): Similar to the upper( ) function in converting case of given characters in the string, except from upper or capitalized letters to lower case characters.

s1="GEEKS"
s2=s1.lower()
print(s2)

islower( ): It returns true if every character in the specified string is in lowercase.

s1="geeks"
print(s1.islower())
Output:
True

isupper( ): It returns true if every character in the specified string is in uppercase.

s1="geeks"
print(s1.isupper())
Output:
False

startswith( ): It checks whether a given string starts with a particular specified sequence.

endswith( ): It checks whether a given string ends with a particular specified sequence.

Both allow specifying the index from which the function should consider it.

s="GeeksforGeeks Python Course"
print(s.startswith("Geeks"))
print(s.endsswith("Course"))
print(s.startswith("Geeks",1))
print(s.startswith("Geeks",8,len(s)))
Output:
True
True
False
True

split( ) & join( ): split( ) function is used to extract a particular word or character sequence from a Python string and convert the separated elements into a list.

s1="geeks for geeks"
print(s1.split())
Output:
['geeks', 'for', 'geeks']

By default the split( ) function splits a string between the spaces present between the characters of a unified string.

However, rather than splitting along the spaces we can specify where exactly or what character should be taken as the ‘separator’.

s1="geeks,for,geeks"
print(s1.split(,))
Output:
['geeks', 'for', 'geeks']

join( ): It acts in a manner opposite to that of the split() function, it takes a list as specified input, and then joins them together into a string.

l=["geeksforgeeks","python","course"]
print(" ".join(l))
Output:
geeksforgeeks python course

We can give the additional parameter for adding an character or sequence to the end of the list elements before they are combined into a string.

l=["geeksforgeeks","python","course"]
print(" ,".join(l))
Output:
geeksforgeeks , python , course

strip(): It allows us to remove undesirable characters from a string. For example if “—CodeKyro—“, we want to remove the hyphens. lstrip() removes them from the left-hand corner while rstrip() removes from the right side.

s="---CodeKyro---"
print(s.strip("-"))
print(s.lstrip("-"))
print(s.rstrip("-"))
Output:
CodeKyro
CodeKyro---
---CodeKyro

split( ) and strip( ) functions are together used in file processing for programs.

find( ): This method is used to search a given string for another string

s1="geeks for geeks"
s2="geeks"
print(s1.find(s2))
print(s1.find("gfg"))
n=len(s1)
print(s1.find(s2,1,n))  

Output:
0
-1
10 

This function is very similar to index function and also takes two additional parameters as values. The difference is that with the index function if the sequence is not found an error is returned, while in find( ) we obtain -1 as the output. In the next article we will explore String Comparison in Python.

Leave a Comment