STRING DATA TYPE
STRING DATA TYPE
Str = "ADITYA"
FORWARD DIRECTION 0 1 2 3 4 5
A D I T Y A
-6 -5 -4 -3 -2 -1 BACKWARD DIRECTION
Hence to access any location of a above given string one can write following
statement:
Str = "ADITYA"
print(Str[1])
output will be : D
Page 1 of 13
STRING DATA TYPE
Creating String
Creation of string in python is very easy.
e.g.
a=‘Computer Science'
b=“Informatics Practices“
str='Computer Science'
print('str -', str)
print('str[0] -', str[0])
print('str[1:4] -', str[1:4])
print('str[2:] -', str[2:])
print('str*2-', str*2 )
print("str+'yes'-", str+'yes')
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
C o m p u t e r S c i e n c e
-16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
OUTPUT
str - Computer Science
str[0] - C
str[1:4] - omp
str[2:] - mputer Science
str*2 - Computer ScienceComputer Science
str+'yes'- Computer Scienceyes
Page 2 of 13
STRING DATA TYPE
Traversal of string: Traversing refers to iterating through the elements of a
string, one character at a time. The function len() is used to find the length
of a string, which includes white spaces also in between of defined string
within single/double/triple quote.
For Example:
Output:
Enter a string = Aditya
============================
Length of the string Aditya is = 6
A a
d y
i t
t i
y d
a A
============================
String comparison :
We can use (>,<,<=,<=,==,!=) to compare two strings .Python compares string
lexicographically i.e using ASCII value of the characters.
Suppose you have str1 as "Maria" and str2 as "Manoj". The first two
characters from str1 and str2 (M and M ) are compared. As they are equal,
the second two characters are compared. Because they are also equal, the
third two characters (r and n ) are compared. And because 'r' has greater
ASCII value than ‘n', str1 is greater than str2.
Page 3 of 13
STRING DATA TYPE
e.g. program
print("Maria"=="Manoj")
print("Maria"!="Manoj")
print("Maria">"Manoj")
print("Maria">="Manoj")
print("Maria"<"Manoj")
print("Maria"<="Manoj")
print("Maria">"")
OUTPUT:
False
True
True
True
False
False
True
Updating Strings:
e.g.
OUTPUT :
Page 4 of 13
STRING DATA TYPE
String Special Operators
e.g.
a="comp"
b="sc"
0 1 2 3 0 1
a= b=
c o m p s c
P y t h o n P r o g r a m m i n g
-18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Output:
Pyth
Python Progra
ho
Programming
Page 6 of 13
STRING DATA TYPE
Example 2:
Output :
Python Programming
Pyth
Python Progra
ho
Programming
Pto
gimroPnhy
Output:
Enter a string = ABPS Renukoot
The Entered String is = ABPS Renukoot
The ABPS Renukoot in reverse order is as ....
tookuneR SPBA
Page 7 of 13
STRING DATA TYPE
Q.# Program to Enter a string and split each word and find its length.
# String Program
str= input(" Enter any string = ")
str1=str.split(' ')
for word in str1 :
print( word," ( ", len(word), ")")
Output:
Enter any string = ABPS Renukoot
ABPS ( 4 )
Renukoot ( 8 )
Q.# Program to enter a string with Punctuation and print them after
removing punctuation from the string.
punctuations = '''!()-[]{};:'"\,<>./?@$%^*_~'''
my_str = input ( " Enter any string : ")
no_punct = ""
for char in my_str:
if char not in punctuations:
no_punct = no_punct + char
no_punct=no_punct.lower()
Output :
Page 8 of 13
STRING DATA TYPE
String functions and methods :
a=“comp”
b=“my comp”
Page 9 of 13
STRING DATA TYPE
Page 10 of 13
STRING DATA TYPE
Output:
Enter a string = Madam
The given string Madam is not a palindrome string
Page 11 of 13
STRING DATA TYPE
output :
Page 12 of 13
STRING DATA TYPE
# Program to check whether given string is palindrome or/not by
revising the string
Output:
Enter any string = madam
The entered string is = madam and it's length is = 5
The reversed string is = madam and it's length is = 5
The given string madam is a Palindrome