Basic String Operations in python

Python String

In Python, Strings are arrays of bytes representing to Unicode characters. Nonetheless, Python doesn’t have a character data type, a single character is just a string with a length of 1. Square brackets can be utilized to get to elements of the string.

Let’s create a string and understand

# Creating a String  
# with single Quotes 
Str1 = 'Welcome to the Programming World'
print("use of Single Quotes: ") 
print(Str1) 
  
# Creating a String 
# with double Quotes 
Str1 = "I love problem solving"
print("\nuse of Double Quotes: ") 
print(Str1) 
  
# Creating a String 
# with triple Quotes 
String1 = '''Hey, nice to meet you!!!'''
print("\nuse of Triple Quotes: ") 
print(String1) 
  
# Creating String with triple 
# Quotes allows multiple lines 
String1 = '''Programming 
            For 
            Life'''
print("\nCreating a multiline String: ") 
print(String1)

Output

String Manipulation using string functions

  • capitalize() : This functions restores the duplicate of the string passed changing the first character of the string to uppercase.

Syntax :

str.capitalize()

Example:

str = "this is how capitalize() function works"
print(str.capitalize())

Output

  • islower() : This function checks the string is in lowercase and returns true if all the characters are in lowercase.

Syntax :

str.islower()

Example

str = "for this string output should be true"
print(str,":",str.islower())
str = "For this string output should be false"
print(str,":",str.islower())

Output

  • isupper() : This method checks whether all the characters present in the string are in uppercase, then it will return true otherwise false.

Syntax:

str.isupper()

Example:

str = "for this string OUTPUT should be false"
print(str,":",str.isupper())
str = "FOR THIS STRING IT SHOULD RETURN TRUE"
print(str,":",str.isupper())

Output

  • lower() : This method converts all the characters of the string to lowercase.

Syntax :

str.lower()

Example:

str = "ENCANTADA DE CONOCERTE"
print(str.lower())

Output

  • upper() : This method converts all the characters to upper case

Syntax :

str.upper()

Example:

str = "WELcome to the world of Python Programming"
print(str.upper())

Output

  • swapcase() : This methods swaps the uppercase character to lowercase character and vice versa, i.e. it swaps all the case of every character.

Syntax :

str.swapcase()

Example:

str = "The capital OF West BENGAL is KOLkata"
print(str.swapcase())

Output

  • len() : This methods counts the total number of characters present in the string. It takes a string as argument whose length you want to calculate.

Syntax :

len(str)

Example :

str1 = "welcome"
print("length = ",len(str1))

Output

  • split() : This function splits the string given as argument on the basis of the separator provided. If nothing is provided as argument, then it splits based on whitespaces.

Syntax :

str.split()

Example :

str = "Python is an easy language"
print(str.split())
print(str.split('an'))

Output

  • replace() : This method returns the copy of the string in which a certain word is replaced by the given word.

Syntax :

str.replace(old,new,chars)

old : the string to replace

new : the new string which is to be replaced

chars : number of characters to be replaced

Example :

str = "Python is an easy language"
print(str.replace('easy','interpreted'))

Output

  • count() : The count method returns the count of occurrence of the substring in the string. 

Syntax :

str.count(sub,start,end)

sub: This is the string to search.

start. Starting index of the search.

end: End index of the search

Example :

str = "I hope you are having a Good Day!!!"
sub = 'a'
print("Number of a in",str, "is", str.count(sub,1,35))

Output

  • lstrip(): This method returns the string after removing all the characters from the beginning of the string. i.e. from the leftside of the string.

Syntax :

str.lstrips(chars)

Example :

str = "!!!!!!!!!!I hope you are having a Good Day!!!"
print(str.lstrip('!'))

Output

  • rstrip() : This method returns the string after removing all the characters from the end of the string. i.e. from the rightside of the string.

Syntax  :

str.rstrip(char)

Example :

str = "!!I hope you are having a Good Day!!!"
print(str.rstrip('!'))

Output

  • rfind() : This method returns the last index of the substring found else -1 if the substring is not present or found.

Synatx :

str.rfind(sub,beg,end)

sub: This is the string to search.

start. Starting index of the search.

end: End index of the search

Example :

str = "  Lets test the function and the test should be good";
print(str.rfind(str))
print(str.rfind(str, 0, 10))
print(str.rfind('test', 0, 20))
print(str.rfind('and', 0, 30))

Output

  • zfill() : Fills the string with a specified number of 0 values at the beginning. If the value of len parameter is less than the length of the string, no filling is done.

Syntax :

str.zfill(len)

Example :

a = "hello"
b = "Python is a very easy language"
c = "10.000"

print(a.zfill(10))
print(b.zfill(10))
print(c.zfill(10))

Output

  • isalnum() : Returns True if all characters in the string are alphanumeric.

Syntax :

str.isalnum()

Example :

str = "User 123"
str1 = "Python"
str2 = "123456"
print(str.isalnum())
print(str1.isalnum())
print(str2.isalnum())

Output

  • isalpha() : Returns True if all characters in the string are in the alphabet.

Syntax :

str.isalpha()

Example :

str = "User 123"
str1 = "Python"
str2 = "123456"
print(str.isalpha())
print(str1.isalpha())
print(str2.isalpha())

Output

  • isdigit(): The method checks whether the strings consist only of digits and returns true or false depending on the string.

Syntax :

str.isdigit()

Example :

str = "Hello user1223455 welcome!!!"
print(str.isdigit())
str1 = "12345678909876543"
print(str1.isdigit())

Output

  • isnumeric() : The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False.

Syntax :

str.isnumeric()

Example :

a = "0AB30"
b = "123456"
c = "678hrs"

print(a.isnumeric())
print(b.isnumeric())
print(c.isnumeric())

Output

  • join() : This method is used to join the string based on the separator given.

Syntax :

str.join(seq)

Example :

seq = "Python", "is", "an", "easy", "language"
str = "-"
print(str.join(seq))

Output

Let’s understand all the the String Operations from a simple example:

str = "Welcome to the world of Python Programming"
print("upper() method:",str.upper())
print("lower() method:",str.lower())
print("islower() method:",str.islower())
print("isupper() method:",str.isupper())
print("len() method:",len(str))
print("isdigit() method:",str.isdigit())
print("rfind() method:",str.rfind(str,0,15))
sub = 'a'
print("'a'count() method:",str.count(sub,0,42))

Output