Strings in Python
Strings in Python
▪ String Operations
▪ Traversing a String
What is Strings?
✓ Strings in Python can be a combination of characters like alphabets and /or digits and /or special
symbols. In other words, a String is a sequence that is made up of one or more UNICODE
characters.
Notes
What is Strings?
✓ A string can be created by enclosing one or more characters in single, double, or triple quotes.
What is Strings?
✓ You can specify multi-line strings using triple quotes (””” or ''').
Example#2:
str3 = “ “ “ Hello World! “ “ “
str4 = ‘ ‘ ‘ Hello World! ‘ ‘ ‘
✓ You can use single quotes and double quotes freely within the triple quotes.
Example#3:
‘ ‘ ‘ This is a multi-line string. This is the first line.
This is the second line.
“ What's your name?,” I asked.
He said "Bond, James Bond ’ ’ ’
What is Strings?
Example #4: Values stored in str3 can be extended to multiple lines using triple codes .
✓ Each individual character in a string can be accessed using a technique called indexing.
✓ Each item in a string corresponds to an index number, which is an integer value, starting with the
index number 0.
✓ The below example shows that the variable “Greetings” has initialized with the string “HELLO” and the
Greetings = H E L L O
0 1 2 3 4
✓ Python supports two types of indexing namely: Positive Index and Negative Index.
✓ Positive Index: This index is used when we want to access the characters of the string from left
to right. This index starts from 0 and increments 1 to all other characters of the string.
✓ Negative index: This index is used when we want to access the characters of the string from right
to left. Start with the index as -1 and the last character has the index –n where n is the length of
the string.
H E L L O W O R L D !
str1 =
0 2 3 4 5 6 7 8 9 10 11
Positive Index
10 Strings in Python | © SmartCliff | Internal | Version 1.0
Introduction to Strings
✓ The index specifies the character to be accessed in the String and is written in square brackets ([ ]).
print(str1[6]) positive
0 1 2 3 4 5 6 7 8 9 10 11
index
'W'
11 Strings in Python | © SmartCliff | Internal | Version 1.0
Introduction to Strings
✓In case, if we give an index value out of this range then the interpreter will raise IndexError.
print(str1[15]) positive
0 1 2 3 4 5 6 7 8 9 10 11
index
✓ The index can also be an expression including variables and operators but the expression must
evaluate to an integer.
Find Length
✓ An inbuilt function len() in Python returns the length of the String that is passed as a parameter.
print(len(str1)) str1 H e l l 0 W o r l d !
positive
0 1 2 3 4 5 6 7 8 9 10 11
12 index
Find Length
print(str1[n-1]) positive
0 1 2 3 4 5 6 7 8 9 10 11
index
'!’
# gives the first character of the string
Negative
-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Index
print(str1[-n] )
str1 H e l l 0 W o r l d !
'H'
String is Immutable
✓ A string is an immutable data type. It means that the contents of the string cannot be changed(add,
✓ If we try to change the string will raise the error called Type error.
str1 H e l l 0 W o r l d !
str1[1] = ‘a’ positive
0 1 2 3 4 5 6 7 8 9 10 11
index
print(str1)
TypeError: 'str' object does not support item assignment
✓ As we know that string is a sequence of characters. Python allows certain operations on string
String Operations
String Concatenation
✓ Python allows to join or concatenate the two strings using the concatenation operator “plus”
which is denoted by the symbol ‘+’.
String Repetition
✓ The “*” operator takes a desired string that needs to be repeated to a particular integer number
and generates a new string.
✓ Example #2: returns the multiple copies of a String and joins them all together.
Membership
✓ The 'in' operator takes two Strings and returns True if the first String appears as a substring in the
second string, otherwise it returns False.
Membership
✓ The not in operator returns True if the element is not present in the String, else it returns False.
String Slicing
✓ In Python, to access some part of a string or substring, we use a method called slicing.
✓ Given a string str1, the slice operation str1[n:m] returns the part of the string str1 starting from
✓ In other words, we can say that str1[n:m] returns all the characters starting from str1[n] till str1[m-1].
✓ The numbers of characters in the substring will always be equal to difference of two indices m and
n, that is (m-n)
String Slicing
'ello’
str1 H e l l 0 W o r l d !
#gives substring starting from 7 to 9
positive
print(str1[7:10] ) index
0 1 2 3 4 5 6 7 8 9 10 11
'orl'
String Slicing
#index that is too big is truncated down to the end of the String
str1 H e l l 0 W o r l d !
print(str1[3:20])
positive
0 1 2 3 4 5 6 7 8 9 10 11
index
'lo World!’
print(str1[7:2])
String Slicing
Example #6: If the first index is not mentioned, the slice starts from index.
'Hello'
Example #7: If the second index is not mentioned, the slicing is done till the length of the String.
print(str1[6:]) positive
0 1 2 3 4 5 6 7 8 9 10 11
index
'World!'
String Slicing
print(str1[-6:-1]) positive
0 1 2 3 4 5 6 7 8 9 10 11
index
'World'
String Slicing
✓The slice operation can also take a third index that specifies the ‘step size’.
✓ For example, str1[n:m:k], means every kth character has to be extracted from the String str1 starting
from n and ending at m-1. By default, the step size is one.
print(str1[0:10:2]) str1 H e l l 0 W o r l d !
'HloWr’ positive
0 1 2 3 4 5 6 7 8 9 10 11
index
print(str1[0:10:3]) str1 H e l l 0 W o r l d !
‘HlWl’ positive
0 1 2 3 4 5 6 7 8 9 10 11
index
String Slicing
Example #9: If we ignore both the indexes and give step size as -1.
#By default, the step size is 1 but that extra colon before the numbers allows us to specify the
str1 H e l l 0 W o r l d !
slicing increment.
positive
0 1 2 3 4 5 6 7 8 9 10 11
print(str1[::2]) index
Hlowrd
str1 H e l l 0 W o r l d !
print(str1[::3]) positive
0 1 2 3 4 5 6 7 8 9 10 11
index
HlWl
Negative
-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
#str1 String is obtained in the reverse order Index
str1 H e l l 0 W o r l d !
print(str1[::-1])
positive
0 1 2 3 4 5 6 7 8 9 10 11
'!dlroW olleH' index
What is traversal?
✓ Processing a String one item at a time. Often, they start at the beginning, select each item in turn,
process and continue until the end. This pattern of processing is called a traversal.
✓ We can access each element of the String or traverse a String using a for loop or a while loop.
Syntax :
for variable in sequence: while condition :
✓ A String is a sequence of items, so the for loop iterates over each item in the String automatically.
Program
# Example: Iterating the sequence of elements using for loop.
1. str1 = 'Hello World!’
2. for ch in str1:
3. print(ch,end = ‘ ')
Hello World! #output of for loop
✓ In the above code, the loop starts from the first character of the String str1 and automatically ends
when the last character is accessed.
33 Strings in Python | © SmartCliff | Internal | Version 1.0
Traversing a String
Trace Table
Line no
str1 ch ch in str1 Output
Trace Table
Line no
str1 ch ch in str1 Output
2 l
2 l in str1→TRUE
3 l
2 o
2 o in str1→TRUE
3 o
2 (space)
2 (space) in str1→TRUE
3 (space)
35 Strings in Python | © SmartCliff | Internal | Version 1.0
Traversing a String
Trace Table
Line no
str1 ch ch in str1 Output
2 W
2 W in str1→TRUE
3 W
2 o
2 o in str1→TRUE
3 o
2 r
2 r in str1→TRUE
3 r
36 Strings in Python | © SmartCliff | Internal | Version 1.0
Traversing a String
Trace Table
Line no
str1 ch ch in str1 Output
2 l
2 l in str1 →TRUE
3 l
2 d
2 d in str1→TRUE
3 d
2 !
2 ! in str1→TRUE
3 !
2 FALSE
37 Strings in Python | © SmartCliff | Internal | Version 1.0
Traversing a String
Program
# Example: Iterating the sequence of elements using while loop.
1. 1.str1 = 'Hello World!’
2.index = 0
3.while index < len(str1):
4. print(str1[index],end = ‘’)
5. index += 1
✓ Use the len() function to determine the length of the String, then start at 0 and loop through the
String items by referring to their indexes.
Trace Table
Line no str1 index index<len(str1) Print index+=1
Trace Table
Line no str1 index index<len(str1) Print index+=1
2 2
3 2 < len (11) →TRUE
4 l
5 3
2 3
3 3 < len (11) →TRUE
4 l
5 4
Trace Table
Line no str1 index index<len(str1) Print index+=1
2 4
3 4 < len (11) →TRUE
4 o
5 5
2 5
3 5 < len (11) →TRUE
4 (Space)
5 (space)
Trace Table
Line no str1 index index<len(str1) Print index+=1
2 6
3 6 < len (11) →TRUE
4 W
5 7
2 7
3 7 < len (11) →TRUE
4 o
5 o
Trace Table
Line no str1 index index<len(str1) Print index+=1
2 8
3 8 < len (11) →TRUE
4 r
5 9
2 9
3 9 < len (11) →TRUE
4 l
5 10
Trace Table
Line no str1 index index<len(str1) Print index+=1
2 10
3 10 < len (11) →TRUE
4 d
5 11
2 11
3 11 < len (11) →TRUE
4 !
5 12
Trace Table
Line no str1 index index<len(str1) Print index+=1
2 12
3 12 < len (11) →FALSE
✓ Python has several built-in functions that allow us to work with strings.
✓ The below table describes some of commonly used built-in functions for string manipulations
len() Returns the length of the String passed as the str1 = 'Hello World!’
argument print(len(str1))
12
title() Returns the String with first letterof every word in str1 = 'hello WORLD!’
the String in uppercase and rest in lowercase print(str1.title())
'Hello World!'
upper() Returns the String with all lowercase letters str1 = 'hello WORLD!’
print(str1.upper())
converted to uppercase. 'HELLO WORLD!
count(str,start, Returns number of times subString str occurs in str1 = 'Hello World! Hello
end) Hello’
the given String. If we do not give start index print(str1.count('Hello',12,25))
and end index then searching starts from index 2
print(str1.count('Hello’))
0 and ends at length of the String 3
rstrip() Returns the String after removing the spaces str1 = ' Hello World!’
print(str1.rstrip())
only on the right of the String. ‘ Hello World!'
strip() Returns the String after removing the spaces str1 = ‘ Hello World! ‘
both on the left and the right of the String. print(str1.strip())
'Hello World!'
✓ We can use format specifiers like other programming languages to perform string formatting. We can
also use the format() method and f-strings to manipulate strings in Python.
✓ Format specifiers are used with the % operator to perform string formatting and when invoked on a
string, takes a variable or tuple of variables as input and places the variables in the specified format
specifiers.
✓ Following is a table of the most commonly used format specifiers for different types of input variables.
Single Character %c
✓ In the below Example ,We put the format specifier in the predefined string at the position where the
variable is to be inserted. Then we insert the variable into the string using the % operator as follows –
✓ We will specify the desired number of format specifiers to insert two or more variables into the
string.
✓ Then we will pass a tuple of variables as input to the % operator at the specified positions in a serial
manner as follows.
myCode = 1117
myName = "Smartcliff"
myStr = "Code of %s is %d" % (myName, myCode)
print(myStr)
# Output: Code of Smartcliff is 1117
✓ Specifying variables with the correct data type for each format specifier is important. Otherwise,
the TyperError exception is raised as follows.
myCode = 1117
myName = "Smartcliff"
myStr = "Code of %s is %d" % (myCode,myName)
print(myStr)
# Output: Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 3, in <module>
myStr = "Code of %s is %d" % (myCode,myName)
TypeError: %d format: a number is required, not str
✓ Specifying the correct number of variables, equal to the number of format specifiers if we insert
more than one variable into the string, is also essential. Inserting variables less than or greater
than the number of format specifiers will again lead to the TypeError exception.
myCode = 1117
myName = "Smartcliff"
myVar=123
myStr = "Code of %s is %d" % (myName,myCode, myVar)
print(myStr)
# output: Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 4, in <module>
myStr = "Code of %s is %d" % (myName,myCode, myVar)
TypeError: not all arguments converted during string formatting
✓ Formatted strings or f-strings are string literals that can be used directly to insert variables into
predefined strings.
✓ To format strings using f-strings, We only need to put variable names into placeholders made using
✓ The value of the variables defined in the placeholders is automatically updated by the Python
myCode = 1117
myName = "Smartcliff"
myStr = f"Code of {myName} is {myCode}"
print(myStr)
# Output: Code of Smartcliff is 1117
✓ f-strings have an advantage over format specifiers: we don’t need to worry about data types of
input variables. We also don’t need to worry about the order of input variables as we have to
specify variable names directly into the placeholders.
✓ In this method, first, we put placeholders in the string in which the variables are to be inserted. Then
we invoke the format method on the string with variables that have to be inserted into the string
as input arguments.
✓ Here, myString is the string containing placeholders for the variables to be inserted. Val1, val2, val3
✓ After execution, the variables are serially placed in the placeholders as follows.
myCode = 1117
myName = "Smartcliff"
myStr = "Code of {} is {}".format(myName,myCode)
print(myStr)
# Output: Code of Smartcliff is 1117
✓ In the format() method, if we pass input arguments greater than the number of placeholders in
myString, Only the variables equal to the number of placeholders are inserted into the string, and
myCode = 1117
myName = "Smartcliff"
myVar=1234
myStr = "Code of {} is {}".format(myName,myCode,myVar)
print(myStr)
# Output: Code of Smartcliff is 1117
✓ If we pass variables less than the number of placeholders in myString, IndexError occurs as
follows.
myCode = 1117
myName = "Smartcliff"
myVar=1234
myStr = "Code of {} is {}".format(myName)
print(myStr)
# Output : Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 4, in <module>
myStr = "Code of {} is {}".format(myName)
IndexError: Replacement index 1 out of range for positional args tuple
✓ The format() method can take input arguments in three ways as follows:
✓ The default way the format() method takes input is that we simply pass the variables as input
arguments.
✓ When the method is invoked on a string, it places the values in the corresponding placeholders in
✓ In this case, we have to pass the input arguments in the same order they have to be inserted into
the string.
# Example : insert a string and an integer variable into a string using default arguments
myCode = 1117
myName = "Smartcliff"
myVar=1234
myStr = "Code of {} is {}".format(myName,myCode)
print(myStr)
# Output:Code of Smartcliff is 1117
✓ Instead of empty placeholders, we can use the positions of the input arguments while inserting them
✓ We fill the index of input variables, in the placeholders, in the string, and during execution, the input
✓ In this method, we are not required to pass the input arguments in the same order they have to be
# Example : insert a string and an integer variable into a string using positional arguments
myCode = 1117
myName = "Smartcliff"
myVar = 1234
myStr = "Variable is {2} and Code of {0} is {1}".format(myName, myCode, myVar)
print(myStr)
# Output:Variable is 1234 and Code of Smartcliff is 1117
✓ We can also use keyword arguments in the format() method for string formatting in Python.
✓ In this method, we define keywords in the placeholders and then pass the input variables as
✓ In this method, we are not required to pass the input arguments in the same order the keywords
✓ This method gives us better flexibility and we can use the variable by placing the keyword
anywhere in the string, irrespective of the position at which it has been defined in the input.
myCode = 1117
myName = "Smartcliff"
myVar = 1234
myStr = "Variable is {myVar} and Code of {myName} is {myCode}".format(myName, myCode, myVar)
print(myStr)
# Output: Variable is 1234 and Code of Smartcliff is 1117
✓ For the alignment of strings, we use the same syntax we used for the alignment of numbers.
✓ To left-align a string, we use the “:<n” symbol inside the placeholder. Here n is the total length of
the required output string.
myString = "Smartcliff"
myStr = "Left Aligned String with length 10 is: {:<10}.".format(myString)
print(myStr)
# Output:Left Aligned String with length 10 is: Smartcliff .
✓ To right align a string, we use the “:>n” symbol inside the placeholder. Here n is the total length of
the required output string.
myString = "Smartcliff"
myStr = "Right Aligned String with length 10 is: {:>10}.".format(myString)
print(myStr)
# Output: Right Aligned String with length 10 is: Smartcliff.
✓ To center align a string, we use the “:^n” symbol inside the placeholder. Here n is the total length
of the required output string.
myString = "Smartcliff"
myStr = "Center Aligned String with length 10 is: {:^10}.".format(myString)
print(myStr)
# Output: Center Aligned String with length 10 is: Smartcliff .
✓ To truncate a string to a specific length, we use the symbol “:.n” inside the placeholder of the
empty string at which the format() method is invoked. Then we pass the input string as the argument
to the format() method as follows: Here, n is the required output length.
myString = "Smartcliff"
myStr = "Truncated String with length 3 is: {:.3}".format(myString)
print(myStr)
# Output: Truncated String with length 3 is: Sma
✓ Alignment is a specific type of padding in which the padding character is the space character. Here
we assume that we have to pad the strings using the * character.
✓ To pad a string on the left side with , we use the “:<n” symbol inside the placeholder. Here n is the
total length of the required output string.
myString = "Smartcliff"
myStr = "Padded String with string on left and length 20 is: {:*<20}".format(myString)
print(myStr)
# Output: Padded String with string on left and length 20 is: Smartcliff**************
✓ To pad a string on the right side, we use the “:*>n” symbol inside the placeholder. Here n is the
total length of the required output string.
myString = "Smartcliff"
myStr = "Padded String with string on right and length 20 is: {:*>20}".format(myString)
print(myStr)
# Output: Padded String with string on right and length 20 is: **************Smartcliff
✓ To pad a string keeping the input string in the center, we use the “:*^n” symbol inside the
placeholder. Here n is the total length of the required output string.
myString = "Smartcliff"
myStr = "Padded String with string on center and length 20 is: {:*^20}".format(myString)
print(myStr)
# Output:Padded String with string on center and length 20 is: *******Smartcliff*******
85 Strings in Python | © SmartCliff | Internal | Version 1.0
String Formatting in Python
✓ Until now, we have used only those formatting types in which the output string size was fixed.
✓ To dynamically set the size of the output string, we can pass the size as input to the format()
method.
✓ For this, we will use an extra placeholder for the size of the output string inside the placeholder for
the input string.
✓ To left-align a string, we use the “:<n” symbol inside the placeholder. Here “n” is the total length of
the required output string.
✓ To dynamically set the size of the output string, we can insert a placeholder in place of “n” and
pass the value “n” as input to the format() method as follows:
myString = "Smartcliff"
length = 20
myStr = "Dynamically Left Aligned String is: {:<{}}".format(myString, length)
print(myStr)
# Output: Dynamically Left Aligned String is: Smartcliff
myString = “Smartcliff"
length = 20
myStr = "Dynamically Right Aligned String is: {:>{}}.".format(myString, length)
print(myStr)
myStr = "Dynamically Center Aligned String is: {:^{}}.".format(myString, length)
print(myStr)
myStr = "Dynamically Padded String with input on left is: {:*<{}}.".format(myString, length)
print(myStr)
myStr = "Dynamically Padded string with input on right is: {:*>{}}.".format(myString, length)
print(myStr)
myStr = "Dynamically Padded string with input on center is: {:*^{}}.".format(myString, length)
print(myStr)
myStr = "Dynamically truncated String with output length 3 is: {:.{}}.".format(myString, 3)
print(myStr)
88 Strings in Python | © SmartCliff | Internal | Version 1.0
String Formatting in Python
Dynamic Formatting using the format() method
# Output:
Dynamically Right Aligned String is: Smartcliff.
Dynamically Center Aligned String is: Smartcliff .
Dynamically Padded String with input on left is: Smartcliff**************.
Dynamically Padded string with input on right is: **************Smartcliff.
Dynamically Padded string with input on center is: *******Smartcliff*******.
Dynamically truncated String with output length 3 is: Sma.
✓ When the specified output length is less than the input string length, the input string is not
altered during the alignment and padding. In case of truncating, the output string is truncated to
the required output length. This can be observed in the following example.
myString = "Smartcliff"
length = 2
myStr = "Dynamically Right Aligned String is: {:>{}}.".format(myString, length)
print(myStr)
myStr = "Dynamically Padded string with input on right is: {:*>{}}.".format(myString, length)
print(myStr)
myStr = "Dynamically truncated String with output length 3 is: {:.{}}.".format(myString, 3)
print(myStr)
# Output: Dynamically Right Aligned String is: Smartcliff.
Dynamically Padded string with input on right is: Smartcliff.
Dynamically truncated String with output length 3 is: Sma.
✓ However, when the given output length is negative, ValueError will occur with a message
“ValueError: Sign not allowed in string format specifier” as seen below.
myString = "Smartcliff"
length = -10
myStr = "Dynamically Right Aligned String is: {:>{}}.".format(myString, length)
print(myStr)
myStr = "Dynamically Padded string with input on right is: {:*>{}}.".format(myString, length)
print(myStr)
# Output:Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/string.py", line 3, in <module>
myStr = "Dynamically Right Aligned String is: {:>{}}.".format(myString, length)
ValueError: Sign not allowed in string format specifier
a) str
b) string
c) text
d) char
Answer : Option a)
a) 'Hello World'
b) "Hello World"
c) '''Hello World'''
Answer : Option d)
a) s * 5
b) repeat(s, 5)
c) s.repeat(5)
d) s.repeat(5)
Answer : Option a)
a) s[0]
b) s.first()
c) first(s)
d) s.charAt(0)
Answer : Option a)
a) count()
b) find()
c) index()
d) replace()
Answer : Option a)
Answer : Option b)
a) s[len(s)]
b) s[-1]
c) s.last()
d) s.charAt(len(s) - 1)
Answer : Option b)
a) index()
b) find()
c) locate()
d) search()
Answer : Option b)
Answer : Option b)
a) s1.concat(s2)
b) s1 & s2
c) s1 + s2
d) concat(s1, s2)
Answer : Option c)