Python string & string functions provide you with a detailed explanation of The python string and its methods.
The string is an immutable sequence of Unicode characters wrapped inside single, double, or triple quotes.
In this blog, you’ll learn:
1.) What is Unicode.
2.) Creating Strings literal in Python.
3.) Printing Strings literal in Python.
4.) String formatting in Python.
5.) Python String methods.
What is Unicode
Unicode is a universal character encoding standard that assigns a code to every character and symbol in every language in the world.
The method to convert decimal numbers to binary numbers is easy. for example, The binary equivalent of 999 decimal number is=1111100111.
But how do you convert characters to binary code? The answer is ASCII and UNICODE.ASCII and UNICODE is the IT standard representing English, Arabic, Greek, mathematical symbols, historical scripts, etc. It assigns a unique code to these characters.
To find Unicode of any character use ord()
function
#for converting/finding unicode of a character. #a_unicode,b_unicode,c_unicode are variables #where we are storing unicode value of different characters. a_unicode=ord('आ') print('unicode of आ=',a_unicode) b_unicode=ord('a') print('unicode of a=',b_unicode) c_unicode=ord('A') print('unicode of A=',c_unicode)
When you run the program, the output will be:
unicode of आ= 2310 unicode of a= 97 unicode of A= 65
To find characters against Unicode use chr()
function
#for converting/finding unicode of a character. #a_char and b_char a_char=chr(2310) print('character value of unicode 2310=',a_char) b_char=chr(97) print('character value of unicode 2310=',b_char)
When you run the program, the output will be:
character value of unicode 2310= आ character value of unicode 2310= a
Creating Strings literal in Python
To create a string in Python, you need to use single quotes, double quotes, or triple quotes. A string can be composed of a single word or multiple words. Python throws exceptions when a single quote is inside a single-quoted string or a double quote inside a double-quoted string.
For example, defining string like ‘Mayank’s favorite language is python’ or ” we should always remember “Honesty is the best policy” “ will throw syntax errors. In order to solve this, Use double quotes and triple quotes.
Incorrect:- ‘Mayank’s favorite language is python’
Correct:- “Mayank’s favorite language is python“
Incorrect:- “we should always remember “Honesty is the best policy” “
correct:- ”’ we should always remember “Honesty is the best policy” ”’
# Single word a_string='Mayank' print('Single word string is=',a_string) # Entire phrase b_string='Mayank is a good boy' print('Multiple word string=',b_string) #python gets confused when there is single quote inside single quote string #c_string='Mayank's favaurite language is python' will throw syntax error #in order to solve this ,python also allows double quote and triple quote # for defining string. # Double quote string c_string="Mayank's favaurite language is python" print('String definition using double quotes=',c_string) #triple quote string d_string=''' "Honesty is the best policy",We should be honest towards our job ''' print('Triple quote string',d_string)
When you run the program, the output will be:
Single word string is= Mayank Multiple word string= Mayank is a good boy String definition using double quotes= Mayank's favaurite language is python Triple quote string "Honesty is the best policy",We should be honest towards our job
String formatting in Python
The print () method is used to print strings literal or variable in python. String formatting gives programmers more options to customize the string-like setting.
String formatting with the help of %
The sign % is used for string formatting in python. %s and %d is used as a placeholder for string and numeric value in string formatting.
sub='Physics' marks=90 print("Mayank got %d marks in %s" %(marks,sub))
Output
Mayank got 90 marks in Physics
String formatting with the help of {}
{} can be used with the format() method for string formatting. {} is replaced with a variable passed in format method in sequence.
sub= 'Maths' percentage= 95 print("Mayank get {} percent in {}".format(percentage,sub))
Output
Mayank get 95 percent in Maths
f-string formatting in Python
A python expression/variable can be embedded inside string literals using F-strings.
# Python3 program introducing f-string val1 =10 val2=20 print(f"The sum of {val1} and {val2} is {val1+val2}.") name='Mohit' age = 25 print(f"Hello, My name is {name} and I'm {age} years old.")
Output
The sum of 10 and 20 is 30. Hello, My name is Mohit and I'm 25 years old.
Python string functions
String datatype in python has a large set of built-in methods. You can use these methods either on string variables or directly on a string.
Capitalize
In Python, the capitalize() method returns a copy of the original string where the first character is in upper case.
a_string='python' print(a_string.capitalize()) var1='python' var2='java' var3='c' #have used python formatting f print(f"Learn {var1.capitalize},{var1.capitalize},{var1.capitalize}")
Output
Python Learn Python,Java,C
upper(),lower()
The lower() and upper() is a built-in string method.upper() method converts string passed into it to upper-case.
Quite opposite lower(), method converts string passed into it to lower-case.
#lower() var1 = 'PYTHONCODINGWITHANISH' print(var1.lower()) #upper() var2 = 'Fusebulbs.Com' print(var2.upper()) print('------------------') #lower(),upper(),+ operator to concatenate strings,\n escape sequence for #new line var3=var1.lower()+'\n'+var2.upper() print(var3)
Output
pythoncodingwithanish FUSEBULBS.COM ------------------ pythoncodingwithanish FUSEBULBS.COM
islower(), isupper()
islower() and isupper() are python built-in methods which returns boolean value(True or False).
It does not accept any argument. Use islower() with the dot operator. islower() will return True if all characters in String it is used with are in lower case otherwise False.
isupper() does not accept any argument. Use isupper() with the dot operator. isupper() will return True if all characters in String it is used with are in upper case otherwise False.
Note:-isupper() and islower() will return False for any string containing only digits, space, or symbol.
#will return true because all characters in string '5a' is in lower case. print('5a'.islower()) #will return true because all characters in string 'abc' is in lower case. print('abc'.islower()) #will return true,all characters in string 'practice python coding ' is in lowercase. print('practice python coding'.islower()) #will return false,all characters in string 'Abc' is not in lower case. print('Abc'.islower()) #will return false because var1(string variable) contains characters in uppercase. . var1='PythonCodingWithAnish' print(var1.islower()) #will return false because all characters in string are digits print('1234'.islower()) #will return false because string contains only space print(' '.islower()) print('-----------------------------') #will return false because all characters in string '5a' is not in upper case. print('5a'.isupper()) #will return false because all characters in string 'abc' is not in upper case. print('abc'.isupper()) #will return false,all characters in string 'practice python coding ' is not in uppercase. print('practice python coding'.isupper()) #will return True ,all characters in string 'ABC' is in upper case. print('ABC'.isupper()) #will return true because var1(string variable) contains characters in uppercase. . var1='PYTHONCODINGWITHANISH' print(var1.isupper()) #will return false because all characters in string are digits print('1234'.isupper()) #will return false because string contains only space print(' '.isupper())
Output
True True True False False False False ----------------------------- False False False True True False False
split()
The method split() accepts a separator as an argument. It then uses the separator to partition the string into a list. By default, Split() takes whitespace as a separator.
#when you don't provide any argument,whitespace willbe used as a seperator var1="Be the change that you wish to see in the world" a_list=var1.split() print(a_list) #passed , seperator as an argument var1="Be the change, that you wish to see in the world" a_list=var1.split(',') print(a_list) #passed - seperator as an argument var1="Be the - change that -you wish to see- in the world" a_list=var1.split('-') print(a_list) #passed 'wish' seperator as an argument var1="Be the change that you wish to see in the world" a_list=var1.split('wish') print(a_list) #split also accepts second argument maxsplit,i.e maximum #no of times you want to split the string/string variable var2 = "python#Java#C#dart#R" print(var2.split("#", 2)) print(var2.split("#", 1))
Output
['Be', 'the', 'change', 'that', 'you', 'wish', 'to', 'see', 'in', 'the', 'world'] ['Be the change', ' that you wish to see in the world'] ['Be the ', ' change that ', 'you wish to see', ' in the world'] ['Be the change that you ', ' to see in the world'] ['python', 'Java', 'C#dart#R'] ['python', 'Java#C#dart#R']
find()
format:- string.find(sub,start,end)
find() method accepts search string as an argument and returns the lowest index of searched string if found. It returns -1 if the string is not found.
#the first instance of search string 'my' is found at index 7,8 #So,it will return 7 a_string='I love my country,my country is great' print(a_string.find('my')) #find() also accepts start and end index as an argument. #it will search the string from index 8 to 25 #and will return the lowest index of 'my' if found print(a_string.find('my',8,25)) #find() will return -1 if searched string is not found in given string. print(a_string.find('python'))
Output
7 18 -1
isalnum()
basic format:- string.isalnum()
The method isalnum() returns true if characters in the string are alphanumeric(alphabets or numbers) and there is at least one character otherwise False.
#will return true because characters in the string 'abc123' and '123' are either #alphabets or numbers. print('abc123'.isalnum()) print('123'.isalnum()) name='anish' print(name.isalnum()) #atleast one element(alphabets or numbers) should be present #in string,otherwise it will return False print(' '.isalnum())
Output
True True True False
isalpha()
basic format:- string.isalpha()
The built-in string method returns true if all characters in the string are alphabets and there is at least one character otherwise False.
#will return true because all elements in string 'abc' are alphabets print('abc'.isalpha()) #false because there is space #all characters should be alphabetic,for returning true name='Anish Kumar Singh' print(name.isalpha()) #false because characters in string '123' are digits print('123'.isalpha()) #all elements in string are not alphabets #will return false print('abc1'.isalpha()) #True because all elements in string are alphabets name='Anish' print(name.isalpha())
Output
True False False False True
isdigit()
basic format:- string.isdigit()
The built-in string method returns true if all characters in the string are digits and there is at least one character otherwise False.
#will return false because all elements in string 'abc' are alphabets not digits print('abc'.isdigit()) #false because there is space #all characters should be digit,for returning true name='Anish Kumar Singh' print(name.isdigit()) #true because all characters are digits print('123'.isdigit()) #all elements in string are not digits #will return false print('abc1'.isalpha()) #True because all elements are digits name='54321' print(name.isdigit())
Output
False False True False True
isspace()
The isspace() is a python built-in method. It returns True if all characters in the string are whitespace characters, Otherwise, It returns False. There must be at least one space character otherwise it will return False.
#contains only space mystring=" " print(mystring.isspace()) #contains character other than space mystring="ab " print(mystring.isspace()) #an empty string,not a single character mystring="ab " print(mystring.isspace())
Output
True False False
lstrip([chars])
lstrip() is built-in method.Based on the chars argument passed, It removes a set of characters from the left side(leading side). If no argument is provided then it removes space from the left side.
#no argument was provided for lstrip,so it will remove space from left mystring=" hello" x=mystring.lstrip() print(x) #This time argument passed is "the",all possible combination #and its reversal ('t','h','e','th','te','he','ht','et','eh') #is mathed from left of string,if found then that pattern is removed. #in present case none of the combination match with left part of string, #so nothing will be removed from left mystring="There" x=mystring.lstrip("the") print(x) #This time argument passed is "The",all possible combination #and its reversal ('T','h','e','Th','Te','he','hT','et','eh') #is mathed from left of string,if found then that pattern is removed. #in present case "The" of the combination match with left part of string, #so "The" will be removed from left mystring="There" x=mystring.lstrip("The") print(x) #This time argument passed is "Te",all possible combination #and its reversal ('T','e','Te','eT') #is mathed from left of string,if found then that pattern is removed. #in present case "T" of the combination match with left part of string, #so "T" will be removed from left mystring="There" x=mystring.lstrip("Te") print(x)
Output
hello There re here
rstrip([chars])
rstrip() is built-in method.Based on the chars argument passed, It removes a set of characters from the right side. If no argument is provided then it removes space from the right side.
#no argument was provided for rstrip,so it will remove space from right mystring="hello " x=mystring.rstrip() print(x) #This time argument passed is "ere",all possible combination and its reversal ('e','r','e','er','ee','re','ere') #is mathed from left of string,if found then that pattern is removed. #in present case "ere" of the combination match with right part of string,so "ere" will be removed from left mystring="There" x=mystring.rstrip("ere") print(x)
Output
hello Th
len() function
len() function is a built-in function in python. Python developers use the len() to get the length of the given string, array, list, tuple, dictionary, etc.
mystring = "Hello" x = len(mystring) print ("The length of string is",x) #it also considers space in calculating length mystring = "Hello " x = len(mystring) print ("The length of string is",x) mylist = ["apple", "banana", "cherry"] x = len(mylist) print("The length of list is",x) mydict={'a':'apple','b':'ball','c':'cat','d':'dog'} x=len(mydict) print("The length of dictionary is ",x)
Output
The length of string is 5 The length of string is 6 The length of list is 3 The length of dictionary is 4
Hope you have learned a lot about Python string, string formatting in python, UNICODE, and string functions.
You have touched upon most important functions.Thanks