Chapter 8 Strings print
Chapter 8 Strings print
» String Operations
» Traversing a String
» Strings Method and Built-in Functions
» Handling Strings
1.STRINGS
String is a sequence which is made up of one or more UNICODE characters.
A string can be created by enclosing one or more characters in single, double or
triple quote.
>>> str1 = 'Hello World!'
>>> str2 = "Hello World!"
>>> str3 = """Hello World!"""
>>> str4 = '''Hello World!''
str1, str2, str3, str4 are all string variables having the same value 'Hello World!'.
String is Immutable
A string is an immutable data type. It means that the contents of the string
cannot be changed after it has been created.
>>> str1 = "Hello World!" #if we try to replace character 'e' with 'a'
>>> str1[1] = 'a'
TypeError: 'str' object does not support item assignment
3. STRING OPERATIONS
String is a sequence of characters.
Python allows certain operations on string data type, such as
concatenation, repetition, membership and slicing.
Concatenation
To concatenate means to join.
Python allows us to join two strings using concatenation operator plus
which is denoted by symbol +
>>> str1 = 'Hello' #First string
>>> str2 = 'World!' #Second string
>>> str1 + str2 #Concatenated strings
'HelloWorld!'
#str1 and str2 remain same
#after this operation.
>>> str1
'Hello'
>>> str2
'World!'
Repetition
Python allows us to repeat the given string using repetition operator
which is denoted by symbol *.
#assign string 'Hello' to str1
>>> str1 = 'Hello'
#repeat the value of str1 2 times
>>> str1 * 2
'HelloHello'
#repeat the value of str1 5 times
>>> str1 * 5
'HelloHelloHelloHelloHello'
Membership:
Python has two membership operators 'in' and 'not in'.
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.
>>> str1 = 'Hello World!'
>>> 'W' in str1
True
>>> 'Wor' in str1
True
>>> 'My' in str1
False
The 'not in' operator also takes two strings and returns True if the first
string does not appear as a substring in the second string, otherwise
returns False.
>>> str1 = 'Hello World!'
>>> 'W' in str1
True
>>> 'Wor' in str1
True
>>> 'My' in str1
False
Slicing
In Python, to access some part of a string or substring, we use a
method called slicing.
This can be done by specifying an index range.
We can say that str1[n:m] returns all the characters starting from
str1[n] till str1[m-1].
>>> str1 = 'Hello World!'
#gives substring starting from index 1 to 4
>>> str1[1:5]
'ello'
#gives substring starting from 7 to 9
>>> str1[7:10]
'orl'
#index that is too big is truncated down to
#the end of the string
>>> str1[3:20]
'lo World!'
#first index > second index results in an
#empty '' string
>>> str1[7:2]
4. TRAVERSING A STRING:
We can access each character of a string or traverse a string using
for loop and while loop.
(A) String Traversal Using for Loop:
>>> str1 = 'Hello World!'
>>> for ch in str1:
print(ch,end = '')
Hello World! #output of for loop
The loop starts from the first character of the string str1 and automatically
ends when the last character is accessed.
(B) String Traversal Using while Loop:
>>> str1 = 'Hello World!'
>>> index = 0
#len(): a function to get length of string
>>> while index < len(str1):
print(str1[index],end = '')
index += 1
Hello World! #output of while loop
Here while loop runs till the condition index< len(str) is True, where
index varies from 0 to
len(str1) -1.