Python String
Python String
Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written inside single, double or triple quotation marks.
- The computer does not understand the characters that we write in our program. Internally, the character or string is encoded in binary language i.e., in the combination of 0s and 1s.
- Each character that we type on computer is encoded with the ASCII or Unicode character encoding in the computer internally.
- So that we can also say that Python string are also the collection of Unicode character encoding system.
- We can create string in our Python program by enclosing the sequence of characters or words in the quotation marks (single, double or triple).
- The string written inside single, double or triple quotation marks in Python is considered same.
Example: 'Hello Python Developers', "Hello Python Developers" and '"Hello Python Developers"' all three are considered same string in Python.
Syntax of Python String
Using the following syntax, we can define a string in our Python program:
string1 = "Hello Python Developers"
The string1 variable holds the group of characters in quotation marks that is a string basically, so when we will check the data type of string1 it will show us string type.
To check the data type of string1 variable, we have to write the following syntax:
print(type(string1)) # Python will show the output as string type
Note: In Python, group of characters are also considered as string data type. This is because Python don't have character data type. A single character like 'M' written in Python program will be considered as string data type with length 1.
Creating a String in Python:
As we know, we can define strings by enclosing in single, double or triple quotation marks. But generally, we don't use triple quotation marks to define string in Python program. Triple quotation marks are usually used for defining multi-line strings that is called as docstrings in our Python program.
Example
# defining a Python string with single quotation mark mkr1 = 'Hello Python Developers !' # defining a Python string using double quotation marks mkr2 = "Hello Python Developers !" # defining a Python string using triple quotation marks mkr3 = "'Hello Python Developers!"' # checking the data type of all the three strings we have defined above print(type(mkr1)) print(type(mkr2)) print(type(mkr3)) # printing all three strings print(mkr1) print(mkr2) print(mkr3)
Output:
class <str> class <str> class <str> Hello Python Developers ! Hello Python Developers ! Hello Python Developers !
Creating a Multiline String in Python:
We can define a multiline string in our Python program using the triple quotation marks only. Let's understand this through an example.
Example:
# Creating a multiline string in our Python program using triple quotation marks mkr = ""' This syntax with the 5 quotation marks will create a multiline string mkr in our Python program"' # checking data type of multiline string print(type(mkr)) # printing the multiline string as output print(mkr)
Output:
class <mkr> This syntax with the 5 quotation marks will create a multiline string mkr in our Python programs
Explanation:
In the above code, we have defined a multiline string mkr. We can define a multiline string in Python by adding two extra quotation marks before initiating string with triple quotation marks.
We don't need to add those two extra quotation marks while ending the string. When we print our multiline string i.e., mkr, Python will print the string output in exact number of line that we have used while defining the string. The data type of multiline string defined by us will also be same in Python i.e., string data type.
Indexing string in Python:
Like any other programming languages, the indexing in Python string begins from 0. The first element or character present in a Python string will be denoted by 0th index.
Let us understand this through an example.
Example: Look at the following string "PYTHON" is indexed in the below code:
mkr = "PYTHON" # indexing of string mkr mkr[0] = 'P' mkr[1] = 'Y' mkr[2] = 'T' mkr[3] = 'H' mkr[4] = 'O' mkr[5] = 'N'
This is the indexing of string mkr which we defined above and that's how string elements are indexed in Python program.
Using the index of a string we can print a specific element present inside a string. Consider the following example with the same string mkr:
# define the mkr string mkr = "PYTHON" # print each element of string mkr using the indexes of element print(mkr[0]) print(mkr[1]) print(mkr[2]) print(mkr[3]) print(mkr[4]) print(mkr[5]) # we will try to print an element with 6 index and since there is no element with index 6, Python will through us an error print(mkr[6])
Output:
P Y T H O N
Traceback (most recent call last): File "C:\Users\Manish\Downloads\code.py", line 11, in <module> print(mkr[6]) IndexError: string index out of range
Explanation:
In the above code, we have defined an index mkr. Then we printed each element of mkr string one by one using the indexes of the element. After that, we have tried to print the element of whose index not exists in string mkr. Python will show us an index error after running the program.
Negative Indexing of the element:
In Python, we can also print the element by using negative indexes. The negative index starts from last element and denoted by -1 index. Negative index is very helpful in Python strings when we have to print elements from last and we don't know the length of the string.
The -1 index represents last element of a string, -2 represents second last element of that string and so on.
Look at the following example with the string mkr:
mkr = "PYTHON"# negatively indexing of string mkr mkr[-6] = 'P' mkr[-5] = 'Y' mkr[-4] = 'T' mkr[-3] = 'H' mkr[-2] = 'O' mkr[-1] = 'N'
Using these negative indexes, we can easily call the elements from last of a string.
Example: Look at the following code for how can we print elements from last using the negative indexes:
# define a string mkr = "TUTORIALANDEXAMPLE" # print elements from last of string mkr using the negative index of the element print(mkr[-1]) print(mkr[-2]) print(mkr[-3]) print(mkr[-4]) print(mkr[-5]) print(mkr[-6])
Output:
E L P M A X
Explanation:
In the above code, we have defined a string mkr with some elements in it. Then we printed the elements from the last using the negative indexes of elements.
The negative Indexing is very helpful when we don't know the exact length of string. We can easily print elements from last using negative indexes without even knowing exact length of the string.
Slicing in Python string:
A slicing operator is used to slice the elements and print a specific number of elements from a string. In Python, slice operator is denoted by []. As we have seen above in indexing of string, how can we access the specific element of a string using this slice operator.
Now, we will learn how to access more than one element from a string using the slice operator. For this, we will use the colon (:) operator inside the slice operator [] to access a substring from the given string.
Let's understand this through an example.
Example:
mkr = "PYTHON" print(mkr[:]) # This line of code will print the whole string "PYTHON" print(mkr[0:]) # This line of code will print the whole string "PYTHON" print(mkr[:6]) # This line of code will print the whole string "PYTHON" print(mkr[0:3]) # This line of code will print the substring "PYT" print(mkr[2:6]) # This line of code will print the substring "THON" print(mkr[1:5]) # This line of code will print the substring "YTHO"
Output:
PYTHON PYTHON PYTHON PYT THON YTHO
Explanation:
In the above code, we have defined a string mkr with some elements in it. Then we have used the colon operator (:) inside the slice operator ([]) to print the substring from string mkr. Then, we have printed the substring using the indexes of elements from mkr string.
Let's look at another example.
Example:
# define a string with elements in it string1 = "TUTORIALANDEXAMPLE" # print substring from string1 using the colon operator (:) inside the slice operator ([]) print(string1[1:6]) print(string1[2:8]) print(string1[3:7]) print(string1[0:4])
Output:
UTORI TORIAL ORIA TUTO
Negative Slicing of String:
We can also perform the slice operation on Python strings using the negative index. The negative indexing in string starts from the rightmost character present in the string. In the negative slicing, we have to write negative indexes of string inside the slice operator and between colon operator to print the substring.
Let's understand this through an example.
Example 1:
mkr = "PYTHON" print(mkr[-6:]) # It will print the whole string PYTHON print(mkr[:-1]) # It will print the substring PYTHO print(mkr[-5:-2]) # It will print the substring YTH print(mkr[-4:-1]) # It will print the substring THO
Output:
PYTHON PYTHO YTH THO
Let's look at another example of negative slicing.
Example 2:
# define a string string1 = "TUTORIALANDEXAMPLE" # print the substring from string1 using negative slicing print(string1[-12:-3]) print(string1[-15:-2]) print(string1[-16:-9]) print(string1[-13:-4]) # printing string1 using one negative index in slice operator print(string1[:-1] print(string1[-19])
Output:
ALANDEXAM ORIALANDEXAMP TORIALA IALANDEXA TUTORIALANDEXAMPL
Traceback (most recent call last): File "C:\Users\Manish\Downloads\code.py", line 3, in <module> print(string1[-19]) IndexError: string index out of range
Explanation:
In the above code, first of all we defined string1 with elements. Then, we printed the substring from string using the negative indexes using the slice operator. Then, we printed the whole string using only one negative index inside slice operator. In the last line of code, Python will show us index because the index is out of the range from string1.
Reassigning the Python strings:
In Python, the string object doesn't support assignment of new items i.e., The content or elements of a string cannot be partially replaced with new elements. We can replace the content of a defined string with completely new content and the new content can be of different size.
- Updating the content/elements present in a Python string is simple as defining a new string.
- Python strings are Immutable in nature i.e., we cannot perform string operations on partial elements of string.
Explanation: we cannot partially update the content or elements of a string.
However, we can completely update or replace the content of string mkr to a completely new content from above example.
Example 2: Look at the following code:
mkr = "TUTORIALANDEXAMPLE" print(mkr) # replace the content of mkr string with a whole new content mkr = "PYTHON" # now print the string mkr print(mkr)
Output:
TUTORIALANDEXAMPLE PYTHON
Explanation: In the above code, we have updated mkr string completely by replacing the content of it by a whole new content. Then we printed the string mkr.
Deleting the String:
As Python strings are immutable in nature, we cannot perform delete operation on partial elements of a string. We cannot delete or remove some specific characters from a string. If we try to delete the specific characters from a string, Python will through as an error.
Let's understand this through an example.
Example 1:
# define a string with mkr name mkr = "PYTHON" # let's try to delete a particular character from mkr using the del keyword del(mkr[2]) # print the string mkr to check if delete operation is performed or not print(mkr)
Output:
Traceback (most recent call last): File "C:\Users\Manish\Downloads\code.py", line 4, in <module> del(mkr[2]) TypeError: 'str' object doesn't support item deletion
Explanation:
When we used del keyword on mkr string, Python through us an error in the output. This is because Python doesn't support delete operation on particular characters of a string.
However, we can delete the complete string from Python.
We can perform delete operation on string and delete it completely from Python memory. We can perform delete operation on Python string using the del keyword on string.
Let's try to delete the entire string mkr from the above example.
Example 2:
mkr = "PYTHON" print(mkr) # delete the entire string mkr using del keyword del(mkr) # check if string is deleted or not by try to printing the string print(mkr)
Output:
PYTHON Traceback (most recent call last): File "C:\Users\Manish\Downloads\code.py", line 6, in <module> print(mkr) NameError: name 'mkr' is not defined
Explanation:
In the above code, we have deleted the entire string mkr from Python memory using del keyword. After that, we have tried to print string mkr and Python will throw an error because no such item is now present in the Python memory.
Operators for Python string
We can perform various operations on string using the given operators in Python. These string operators can concatenate, multiply and do many other operations with string. Following are the list of string operators present in Python:
1. Concatenate operator (+)
2. Repetition operator (*)
3. Slice operator ([])
4. Slice operator with colon ([:])
5. Membership operator ('in' and 'not in')
6. Raw string operator (r/R)
7. String formatting operator (%)
Let's discuss about each of the string of the string operator mentioned above.
1. + Operator (concatenate operator):
The '+' denotes the concatenate operator and perform concatenation operation on strings in Python. The concatenate operator is used to add two or more strings.
We can use the concatenate operator in two following ways:
- Add two strings and assign its value to a third string
- Concatenate two string while printing the output
Let's understand this through an example.
Example 1:
# define two strings with some elements in it mkr1 = "Hello" mkr2 = "Python Developers" # define a third string with concatenation of first two strings mkr = mkr1+mkr2 # print the third string to check if the concatenated value is assigned to it or not print(mkr)
Output:
Hello Python Developers
Explanation:
As we can see, we have defined two strings then we have added them and assigned the value to third string i.e., mkr. Then we printed the mkr string. The mkr string holds the concatenated value of two strings.
Example 2:
# define two strings with some elements in it mkr1 = "Hello" mkr2 = "Python Developers" # concatenate the strings mkr1 and mkr2 while printing the output print(mkr1+mkr2)
Output:
Hello Python Developers
2. Repetition operator (*):
The '*' symbol denotes the repetition operator in Python and it repeats the strings. The repetition operator is used to repeat the string value. We can also assign the repetition value of a string to another string.
Example: Look at the following code:
# define a string mkr = "Hello Python Developers" # use the repetition operator on mkr string while printing the output print(mkr*4)
Output:
Hello Python DevelopersHello Python DevelopersHello Python DevelopersHello Python Developers
Explanation: In the above code, we have defined a string mkr and used repetition operator on it. When we used repetition operator on string while printed, Python printed the mkr string 4 times as we have given in code.
3. Slice operator ([]):
As we have discussed earlier about slice operator, it is used for slicing of a string. The close square brackets '[]' represents slice operator in Python. Slice operator is used to print a particular element from a given string. For that we need to specify the index of the element which we want to print in output.
Example: Look at the following code:
mkr = "Hello PYTHON Developers"
Output:
l T e
4. Slice operator with colon ([:]):
The colon is used inside a slice operator to print substring from a given string. We can also use negative indexes inside the slice operator while printing substring with colon.
Example: Look at the following code:
string1 = "TUTORIALANDEXAMPLE" # print the substring from string1 using the slice operator with colon print(string1[1:6]) print(string1[2:8]) # print substring from slice operator while using the negative indexes with colon print(string1[-7:-2])
Output:
UTORI TORIAL EXAMP
5. Membership operator (in and not in):
We can check the membership of a substring from a string using the membership operator. The membership operators are used to check if a particular substring belongs to the string defined in our program or not. Membership operators are of two types:
(i) 'in' membership operator
(ii) 'not in' membership operator
Let's discuss about both types of membership operator with one example of each.
(i) 'in' membership operator: The 'in' operator is a type of membership operator. It is used to check if a particular substring is present in the string defined in our program. The output given by 'in' operator is same of boolean operator. It gives output as either true or false.
Syntax for 'in' operator:
print(substring 'in' string)
Example: Look at the following code:
# define a string mkr = "TUTORIALANDEXAMPLE" # Use 'in' operator to check if 'AND' and 'APE' substrings are present in mkr print('AND' in mkr) # It will prints true as AND is present in mkr print('APE' in mkr) # It will print false as APE is not present in mkr
Output:
True False
(ii) 'not in' membership operator: The 'not in' operator is also a type of membership operator. It is used to check if a particular substring is not present in the string defined in our program. The output given by 'not in' operator is also same of boolean operator. It gives output as either true or false.
The 'not in' operator always gives opposite output of 'in' operator i.e., If 'not in' gives False then 'in' operator will give true in output or vice-versa.
Syntax for 'not in' operator:
print(substring 'not in' string)
Example: Look at the following code:
# define a string mkr = "TUTORIALANDEXAMPLE" # Use 'not in' operator to check if 'ACE' and 'TOR' substrings are not present in mkr print('ACE' not in mkr) # It will print true as ACE is not present in mkr print('TOR' not in mkr) # It will print false as TOR is present in mkr
Output:
True False
6. Raw string operator (r/R):
The raw string operator is used to specify a raw string in our Python program. Raw strings are the strings where we need to print actual or as it is meaning of the escape characters (We will talk about escape characters present in strings later in this section) of string such as "C://Manish/Python" etc.
Syntax for raw string:
To define any string in as a raw string in Python, the character of string should be followed by 'r' or 'R.' r'string' or R'string'
Let's understand this through an example.
Example:
# printing raw strings with raw string operators i.e., r and R print(R'C://Python39') # the 'R' keyword makes C://Python39 as raw string and will print it as it is written print(r'C://Manish/Python') # the 'r' keyword makes C://Manish/Python as raw string and will print it as it is written
Output:
C://Python39 C://Manish/Python
7. String formatting operator (%):
The '%' symbol denotes the string formatting operator in Python. The string formatting operator is used to perform formatting operations on string. The string formatting operator makes use the format specifiers from the C programming languages such as %f or %d etc.
- The format specifiers are used by string formatting operators to map their values in Python.
- We will discuss about the how formatting is done through string formatting operator on Python strings.
Example: Look at the following Python program with string formatting operator:
# define a string mkr mkr = "Hello Python Developers" # use the string formatting operator on mkr while printing the output print("The formatted string mkr is : %s"%(mkr)) # This line of code will print 'The formatted string mkr is : Hello Python Developers' as string formatting operator is used on mkr
Output:
The formatted string mkr is : Hello Python Developers
Now, we will discuss about Python string formatting and escape characters in Python string in detail.
Python string Formatting
Escape sequence in Python string:
Let's suppose we have to write or print the following string in our Python program:
Mike said, "Hello People, what's going on there?"
The above statement can be written in single or double quotation marks but it will raise the syntaxerror in our program because both single and double quotes are already present in the above statement.
We can't even use triple quotation marks with such sentences because even use of triple quotation marks with such sentences causes 'syntaxerror' while defining strings in our Python program.
Let's use this sentence in a Python program to understand it better.
Example: Look at the following code to understand the concept of use of Python string operators:
# define a string with above given sentence mkr= "Mike said, "Hello People, what's going on there?"" # print the mkr string print(mkr) # This will cause a syntaxerror while running the given program
Output:
SynatxError: The ‘mkr’ string have syntax error in it and not defined properly
We can solve this problem by using triple quotation marks in this sentence. But, use of triple quotation marks is also limited. Triple quotation marks cannot be valid with every given sentences.
Therefore, Python provides us the escape sequence which is very useful in such problems.
The backlash symbol (\) represents the escape sequence present in Python.
We can use this backlash symbol (\) with a special character and it will be interpreted differently in Python.
Different special characters used with escape sequence (\) represents different functionalities and different uses.
Escape sequence (\) escapes the interpretation of some characters, quotation marks (single, double), special characters etc. present in the given sentence.
Now, we will apply both triple quotation marks and escape sequence in the above given sentence and print it as output.
Example: Look at the following code to understand the use of escape sequence in Python program:
# print the sentence using the triple quotation marks in print statement print('''''Mike said, "Hello People, what’s going on there?"''') # print the sentence using the escape sequence with the single quotation mark to escaping single quote in sentence print('Mike said, "Hello People, What\'s going on there?"') print("Mike said, \"Hello People, What's going on there?\"")
Output:
''Mike said, "Hello People, What's going on there?" Mike said, "Hello People, what’s going on there?" Mike said, "Hello People, what’s going on there?"
Explanation:
In the above code, first we printed the given sentence using the triple quotation marks. The triple quotation marks defined the string type in print statement so that single and double quotes both are printed in output without throwing an error in program.
Then, in second print line we used escaping sequence on single quote in sentence. Therefore, we have used single quotation mark while printing the statement. In the last print line, we used escape sequence on double quotation marks for escaping double quotes from sentence.
Therefore, we have used double quotation marks to define the sentence in print statement. Using escape sequence will solve the syntaxerror problem even if we define our sentence with single or double quotes.
Few Special Escape Sequence
As we have mentioned earlier, we can use some special characters with escape sequence and Python will interpret them differently. Using different special characters with escape sequences represents different functionalities of escape sequence and they are used differently according to the need of program.
Following is the list of all escape sequences in Python used with different special characters:
1. \newline
2. \\
3. \'
4. \"
5. \a
6. \b
7. \f
8. \n
9. \r
10. \t
11. \v
12. \ooo
13. \xHH
Here is an example of escape sequence in Python program and how they are used.
Example: Look at the following code:
# using the '\\' escape sequence in the print statement print("C:\\Users\\MANISH ARORA\\Python39\\Code") # using the '\n' escape sequence in the print statement print("This is a sentence which \n will print in multiline \n in the Python output \n when we will run the program") # using the '\xHH' escape sequence in the print statement print("This is \x48\x45\x58 representation")
Output:
C:\Users\MANISH ARORA\Python39\Code This is a sentence which will print in multiline in the Python output when we will run the program This is HEX representation
Now, let's look at the each of escape sequence and their use in Python program.
1. \newline: This escape sequence is used when the code is written in multiple lines. It will ignore the new or multiple lines when used with the line of code. We can use this even in our print statement and then write command in multiple lines.
Example: Look at the following code:
# use the \newline escape sequence in print statement and write command in multiple lines within print statement print("Hello \ Python \ Developers") # The output will be printed in single line as 'Hello Python Developers'
Output:
Hello Python Developers
2. \\: This print statement is used to print backlash itself in the print statement otherwise Python will ignore the backlash while interpreting the code.
Example: Look at the following code:
# Use the '\\' escape sequence in print statement to print backlash print("\\") # it will print only single backlash symbol # use the '\\' in another example print("C:\\Users\\MANISH ARORA\\Python39\\Code")
Output:
C:\Users\MANISH ARORA\Python39\Code
3. \': This escape sequence will print the given sentence and ignore the interpretation of single quote within the sentence. This escape sequence solves the problem of syntax error where both single and multiple quotation marks are given in a sentence.
Example: Look at the following code:
# use the \' escape sequence in print statement to print single quote print('\'') # it will print a single quote (') # using the \' escape sequence within a sentence given print('Mike said, "Hello People, What\'s going on there?"')
Output:
'
Mike said, "Hello People, what’s going on there?"
4. \": This escape sequence will print the given sentence and will ignore the interpretation of double quotes given in the sentence.
Example: Look at the following code:
# use the \" escape sequence in print statement to print single quote print("\"") # it will print a single quote (") # using the \" escape sequence within a sentence given print("Mike said, \"Hello People, what’s going on there?\"")
Output:
"Mike said, "Hello People, what’s going on there?"
5. \a: This is a special escape sequence. It represents ASCII Bell (Bel). This sequence will print nothing but makes ringing the bell alert sound when used in Python program. (E.g., xterm etc.)
Syntax:
print("\a")
6. \b: This escape sequence is used to represent ASCII backspace (BS) in Python. This escape sequence removes the previous character in a sentence where it is used. As the representation suggestion, it works as a backspace within the print statement.
Example: Look at the following code:
# print a sentence while using \b escape sequence in print statement: print("Hello\b Python\b Developers”) # it will print 'Hello? Python? Developers' as each escape sequence will remove one previous character from sentence.
Output:
HelloPythonDevelopers
7. \f: This escape sequence represents ASCII form feed in Python. It will add white space within the words and print them in separate pages when used in a sentence. We can use \f escape sequence in the print statement.
Example: Look at the following code:
# print a sentence using \f escape sequence in it print("Hello\fPython\fDevelopers") # It will print "Hello Python Developers" in the output and each word in different pages
Output:
Hello Python (in different page) Developers (in different page)
8. \n: This escape sequence represents ASCII linefeed in Python program. It is used to add an extra line in the sentence. We can use this \n escape sequence within a sentence in the print statement to print the sentence in multiple lines as output.
Example: Look at the following code:
# using the '\n' escape sequence in the print statement to print the given sentence in multiple lines print("This is a sentence which \n will print in multiline \n in the Python output \n when we will run the program")
Output:
This is a sentence which will print in multiline in the Python output when we will run the program
9. \r: This \r escape sequence moves all the characters after the beginning of the line while overriding the same number of characters that are moved within print statement. This escape sequence represents ASCII Carriege Return (CR) in Python.
Example: Look at the following code:
# use the \r escape sequence in a sentence within the print statement print("Hello \rPython Developers!") # It will print Python Developers in the next line of output of the program
Output:
Hello Python Developers
10. \t: This \t escape sequence is used to represents ASCII horizontal tab in Python program. When we use this escape sequence within a sentence, it will add a tab space between the words of that sentence.
Example: Look at the following code:
# use the '\t' escape sequence in print statement between the words print("Hello \t Python \t Developers") print("It is an escape \t sequence statement") # This line of code will print "It is an escape sequence statement"
Output:
Hello Python Developers It is an escape sequence statement
11. \v: This \v escape sequence is used to represents ASCII vertical tab in Python program. When we use this escape sequence within a sentence, it will add a vertical tab space between the words of that sentence. It means \v escape sequence add an extra line between the words where it is used in sentence.
Example: Look at the following code:
# use the '\t' escape sequence in print statement between the words print("Hello Python \v Developers") # This line of code will print "Hello Python (In next line) Developers" because of ASCII Vertical tab formatting
Output:
Hello Python Developers
12. \ooo: This escape sequence is used to decode the words that are encoded in octal value format. The sentence or words are written in octal value format and \ooo escape sequence decodes them in alphabetical words and prints the output after decoding.
Let's understand the working of \ooo escape sequence through an example.
Example:
# use the '\ooo' escape sequence in print statement so that octal values can be decoded and printed as words in output print("\110\145\154\154\157") # This octal value format will be decoded and printed as "Hello" in output after decoding
Output:
Hello
13. \xHH: The '\xHH' escape sequence works very similar to the \ooo escape sequence. This escape sequence is used to decode the characters that are encoded in the hex value formats. The sentence or words are written in hex value format and \xHH escape sequence decodes them in alphabetical words and prints the output after decoding.
Let's understand the working of \xHH escape sequence through an example.
Example:
# use the '\xHH' escape sequence in print statement so that hex values can be decoded and printed as words in output print("\x48\x65\x6c\x6c\x6f") # This hex value format will be decoded and printed as "Hello" in output after decoding print("This is \x48\x45\x58 representation") # This line of code will print "This is hex representation" in the output after decoding
Output:
Hello This is HEX representation
The String format() method:
The string format() method is the very helpful and flexible method in formatting of strings within a Python program. In the format method, we use curly braces as the placeholders inside the print statement or string and formatting is done by replacement using format() method argument.
The format() method of string in Python is very helpful when we want to modify string just before printing it in output.
Let's look at an example of format() method argument.
Example:
# Using the format() method on a string while using curly braces as placeholders print("{} and {} both are best friend since 6th class of school".format("Manish Arora","Gaurav Panwar")) # The string will be printed only after formatting done by format() method
Output:
Manish Arora and Gaurav Panwar both are best friend since 6th class of school
Explanation:
In the above code, we have used the format() method on string within the print statement. We used curly braces as placeholders in the string and then we placed values from format i.e., "Manish Arora" & "Gaurav Panwar". Python printed the output string after performing format() string argument on curly braces.
We can use the curly braces in two ways as placeholders in the string while using format() method argument. The two ways of using curly braces as placeholders are following:
1. Using digits inside curly braces for positional argument in format() method:
In this method, we use numerical digits inside curly braces to perform positional argument on string with format method() in print statement.
Example: Look at the following code:
# performing positional argument on string while using numerical values inside curly braces for format() method print("{1} and {0} both are best cricket players in all of men cricketer right now".format("Virat Kohli","Steve Smith"))
Output:
Steve Smith and Virat Kohli both are best cricket players in all of men cricketer right now
2. Using alphabets inside curly braces for keyword argument in format() method:
In this method, we use alphabets value inside curly braces to perform keyword argument on string with format() method in print statement. We will assign element value to alphabets in the format() method to use them in curly braces.
Example: Look at the following code:
print("{a} and {b} both are living in the same city since they were born". format(a = "Vizag Kans",b = "Steven Rogers")) # while printing the string in output Python will a with Vizag kans and b with Steve rogers
Output:
Vizag Kans and Steven Rogers both are living in the same city since they were born
Formatting of Python string using the '%' operator:
In Python, we are allowed to use the string format specifiers that we use in printf statement in C to format a particular string. The format specifiers that we use with strings in Python are treated or operated in the same way as they are treated in C language. The '%' operator is called as format specifier in Python. We use keywords with it perform operation on Python string with format specifiers.
- Moreover, Python provides an extra operator i.e., the '%' operator.
- The '%' operator is used as an interface between the format specifiers of Python strings and value allocated to them.
- It means we can say that the '%' operator binds format specifiers of Python strings to their values allocated.
Let's understand the concept of '%' operator through an example.
Example: Look at the following code:
# define an integer value variable Integer_value = 24; # define a float value variable Float_value = 22.983 # define a string value variable String_value = "TUTORIALANDEXAMPLE" # use the '%' operator in the print statement to print the integer, float and string value in the output of code print("Hello I am an Integer variable ... The value assigned to me is %d\nHello I am a float variable ... The value assigned to me is %f\nHello I am a string variable ... The value assigned to me is %s"%(Integer_value,Float_value,String_value))
Output:
Hello I am an Integer variable ... The value assigned to me is 24 Hello I am a float variable ... The value assigned to me is 22.983000 Hello I am a string variable ... The value assigned to me is TUTORIALANDEXAMPLE
Explanation: In the above code, firstly we have defined three variables with integer, float and string values respectively. Then, we printed the variables with the print statement using the % operator. The % operator assigned the variables to respective sentences in the print statement as we can see that. The % operator is also used to define the data-types of variable while assigning variables to respective sentences.
Python String Functions
Like many popular programming languages, Python also provides various built-in functions for strings. These built-in functions are used for string handling in Python. Following are the many built-in string functions that are present in Python:
capitalize() | The 'capitalize()' function capitalize the first character of the given string. The 'capitalize()' function for string is deprecated in Python 3.x versions. |
carefold() | The 'carefold()' function returns the versions of s suitable for care-less comparisons between strings and assigned values. |
rpartition() | The 'rpartition()' function spilits a given Python string from the last occurrence of separator substring of given parameter. This method returns a tuple after performing split operation on the given string. |
centre(width,fillchar) | The 'centre(width,fillchar)' function returns a space padded string after performing operation on it. This method returns a space padded string that is centred with equal number of spaces on left and right of string. |
zfill(width) | The 'zfill(width)' function returns the original string on which operation is performed leftpaded with zeroes to a total width character given in the function. This function retains any sign given in tje the string (less one zero) that is intended for numbers only. |
count(string name, begin, end) | This 'count()' function is used to count the total number of occurrences of a substring in the given string inside the function. This function counts the occurrence of substring between the beginning and end index we defined in the function. |
upper() | The 'upper()' function in Python is used to convert or change all the characters of the given string in the upper case form. |
decode(encoding = 'UTF8', errors = 'strict') | The 'decode(encoding = 'UTF8', errors = 'strict')' function decodes the given string using the codec registered for encoding form of string. |
translate(table,deletechars = *) | The 'translate(table,deletechars = *)' function translates a given string according to the translation table that we have passed in the function. |
encode() | The 'encode()' function encodes the given string using the codec registered for encoding of strings in Python. The default codec registered for encoding of Python string is 'UTF-8'. |
title() | The 'title()' function is used to convert the given string inside the function to a title-case form. The given string PaLWal will be converted to Palwal. |
endswith(suffix,begin = 0,end = len(string)) | The 'endswith(suffix,begin = 0,end = len(string))' function returns a boolean value i.e., True or False if the given string in the function terminates with the suffix given between the begin and end of function value. |
swapcase() | The 'swapcase()' function is used to inverts case all the characters of the given string in the function. The string Palwal will be sorted to lawlaP in swapcase(). |
expandtabs(tabsize = 8) | The 'expandtabs(tabsize = 8)' function define tha tabs in the given string to multiple spaces. The default space value inside this function is 8 for a string. |
strip([chars]) | The 'strip([chars])' function is used to perform lstrip() and rstrip() function on the given string in Python program. |
find(substring, beginIndex, endIndex) | The 'find(substring, beginIndex, endIndex)' function returns the index value of the string where the given substring in the function is find in the string on which operation is performed. The operation is performed on the string from the begin Index and end Index defined inside the function. |
startswith(string,begin = 0,end = length(string)) | The 'startswith(string,begin = 0,end = length(string))' function returns a boolean value if the given string starts with the 'string' value defined in the function between the beginning and end of index. |
format(value) | The 'format(value)' function returns the formatted version of the given string using the passed value defined in the function. |
splitlines(num=string.count('\n')) | The 'splitlines(num=string.count('\n'))' function returns the total list of string present in each line with the newline removed from the string after performing operation on it. |
index(substring, beginIndex, endIndex) | The 'index(substring, beginIndex, endIndex)' function throws an exception if the given substring is not found in the given string between the begin index and end index defined inside the function. This function works same as of 'find(substring, beginIndex, endIndex)' function. |
split(str, num = string.count(str)) | The 'split(str, num = string.count(str))' function splits the given string according to the delimiter str we have defined within the function. The string is splited according to the space in string if the delimiter is not provided in the function. This function returns list of substrings concatenated with the delimiter provided inside the function. |
isalnum() | The 'isalnum()' function return true after performing operation if the given characters in the string are in the form of alphanumeric values. There should be alteast one character in string for the function to return true in output. Otherwise, in all other cases, this function will return false in output. |
rsplit(sep = None, maxsplit = -1) | The 'rsplit(sep = None, maxsplit = -1)' function works in the same process as of 'split(str, num = string.count(str))' function but this function process the given string from the backward direction. This function returns the list of total words present in the given string. If the separator is not specified within the function, then the function splits the string according to the white-space present in the string. |
isalpha() | The 'isalpha()' function returns true in output only if all the characters present in the given string are alphabets and there should be at least one character is present in the string. Otherwise, this function will return false in the output. |
rstrip() | The 'rstrip()' function removes all the trailing white spaces present in the given string. This function is also used to remove a particular character in trailing of the given string. |
isdecimal() | The 'isdecimal()' function returns true in the output if all the characters present inside the given string are decimals. Otherwise, this function will return false in output. |
rjust(width,[fillchar]) | The 'rjust(width,[fillchar])' function returns a space padded string in the output if having the original string right justified to the total numbers of characters specified in the given original string. |
isdigit() | The 'isdigit()' function returns true in the output if all the characters present in the given string are digits and there should be atleast one character is present in the string. Otherwise, this function will return false in the output. |
rindex(str,begin = 0,end = length(str)) | The 'rindex(str,begin = 0,end = length(str))' function works same as of index() function but this function also transverse the given string in the backward direction in the output. |
isidentifier() | The 'isidentifier()' function returns true in the output if the given string in the function is a valid identifier. |
rfind(str,begin = 0,end = length(str)) | The 'rfind(str,begin = 0,end = length(str))' function works very similar to the find() function works on string but this function transverse the given string in backward direction after performing the operation on it. |
islower() | The 'islower()' function returns true in the output if all the characters present in the given string are in lower case form ,otherwise it will return false in the output. |
replace(old,new[,count]) | The 'replace(old,new[,count])' function replaces the old sequence of character present in the given string to the new sequence of characters defined in function. The maximum character are replaced from the string if max is given in the function. |
isnumeric() | The 'isnumeric()' function returns true in output only if the given string contains all the characters are in numeric form otherwise, this function will print false in output. |
maketrans() | The 'maketrans()' function return a translation table in the output according to the given string that to be used in the translate function. |
isprintable() | The 'isprintable()' function return true in output if all the characters of given string in function are printable or the given string is empty, otherwise this function will print false in output. |
partition() | The 'partition()' function searches for the separator sep in the given string 'S' inside the function and returns the part of the string before the separator, the separator 'sep' itself and the part after the separator in output. If the separator 'sep' is not found in the given string then this function will return the original string S and two empty strings in the output. |
isupper() | The 'isupper()' function returns true in output if all the characters of the given string in function are in the upper-case form, otherwise this function will return false in output. |
Istrip() | The 'Istrip()' function removes all the leading white spaces present in the given string in function. This function is also used to remove a particular character in the given string from leading. |
isspace() | The 'isspace()' function returns true in output if the given string in function only contains white spaces in it, otherwise this function will return false in output. |
istitle() | The 'istitle()' function return true in output only if the given string in function is in proper titled form, otherwise this function will return false in output. This function will return true for Palwal string but false for all other form of it (palwal, PALWAL etc.) |
lower() | The 'lower()' function is used to change all the characters of the given string in function in the lower case form. |
upper() | The 'upper()' function is used to change all the characters of the given string in function in the upper case form. |
join(seq) | The 'join(seq)' function used to merge the given string's representation in the given sequence provided in the function. |
Ijust(width[,fillchar]) | The 'Ijust(width[,fillchar])' function returns the space padded string in the output with the original string given is left justified to the given width provided in the function. |
len(string) | The 'len(string)' function returns the length of the given string provided in the function in the output. |