STRINGS
• String is data type that stores a sequence of characters.
• String is the collection of the characters surrounded by single quotes, double quotes, or
triple quotes.
• The computer does not understand the characters directly; instead, they process and
store all data as binary (0s and 1s).
CREATING STRING IN PYTHON
#Using single quotes
str1 = 'Hello Python'
print(str1)
#Using double quotes
str2 = "Hello Python"
print(str2)
#Using triple quotes
str3 = '''Triple quotes are generally used for
represent the multiline or
docstring'''
print(str3)
OPERATION ON STRINGS
• Concatenation:
“Hello” + “world” = “Helloworld”
• Length of a String:
• fruit = “Mango”
• len1 = len(fruit)
• print(“Mango is a” , len1 , “letter word.”)
INDEXING
H E L L O
0 1 2 3 4
str = “HELLO”
str[0] is ‘H’ , str[1] is ‘L’ ……
SLICING
• Accessing parts of a string
str[ starting_idx : ending_idx ] #ending idx is not included
str = “Damini”
str[ 1 : 3 ] is “am”
str[ : 3 ] is same as str[ 0 : 3]
str[ 1 : ] is same as str[ 1 : len(str) ]
#Negative index:
A P P L E
-5 -4 -3 -2 -1
str = “APPLE”
str[ -3 : -1 ] is “PL”
EXAMPLE:
fruit = “apple”
len1 = lan(fruit)
print(len1)
LOOP THROUGH A STRING
alphabets = "String is data type that stores a sequence of characters."
for i in alphabets:
print(i)