XI STRINGS NOTES FOR BOOK
XI STRINGS NOTES FOR BOOK
Strings are contiguous series of characters enclosed in single or double quotes. Python doesn’t
have any separate data type for characters so they are represented as a single character string.
For eg:
>>> s_name = “Amit” # s_name is a variable storing a string.
>>>s = ‘a’ #String can also be enclosed in single quotes
How to Create Strings in Python?
Creating strings: To create string enclose the character or sequence of character in Single or Double
Quotes like shown below
>>> s_name = “Amit” #String enclosed in double quotes
>>>s = ‘a’ # String enclosed in Single Quotes
We can also use str() function to create string:
N = str () # This function will create an empty string
name = str(1234) # This will create a variable name which store a string “1234”
If we execute the following code:
>>>print(name)
Output will come 1234
TRAVERSING STRINGS IN PYTHON:
Traversing a String: It means accessing all the elements of the string one by one using index value.
St = “PYTHON”
Strings in Python
St[1] = Y St[5] = N St[-1] = N St[-6] = P
Membership Operators :
Membership Operator : OUTPUT
There are two membership operators:
1. ‘in’ operator returns True if a substring is present in main string
2. ‘not in’ operator returns True if a substring is not present in main string.
str1 = “Welcome”
print(‘com’ in str1) True
print(‘W’ in str1) True
print(‘Wel’ not in str1) False
String Comparison :
OUTPUT
We can compare the string using relational/comparison operator like
(>, <, >=, <=, ==, !=)
False
print(“amit” == “Amit”)
True
print(“amit” != “Amit”)
False
print(“blog” > “z”)
String slicing in Python
String Slicing: It means to get a slice from a main string. In simple
words we can say that slicing is to get a piece of substring from the
main string.
Syntax: string-name[start:end:step]
(Here end is excluded)
str1= "Informatics"
print(str1[1:5]) nfor
print(str1[3:9]) ormati
print(str1[-10:-5]) nform
print(str1[:]) Informatics
print(str1[:6]) Inform
print(str1[::2]) Ifrais
print(str1[::-1]) scitamrofnI
This function
str = ” I am Learning Python”
upper() converts the string I AM LEARNING PYTHON
print(str.upper())
into uppercase
This function
converts the str = ” I am Learning Python”
lower() i am learning python
string into print(str.lower())
lowercase
This function
replaces a substring
from the main
str = ” I am Learning Python”
string.
newstr=str.replace
replace() As strings are I am Doing Python
(“Learning”,”Doing”)
immutable
print(newstr)
so this function
creates a new
string
This function
return the index of str = “I am Learning Python”
find() 2
substring in a main print(str.find(‘am’))
string.
This function
str = “I am Learning Python”
len() returns the length 20
print(len(str))
of the string.
This function
counts the number str=”I am Learning Python”
2
count() of occurrences of a print(str.count(‘a’))
3
substring in main print(str.count(‘n’))
string
This function
converts the first
alphabet of the
str=”I am Learning Python”
capitalize() string to upper case I am learning python
print(str.capitalize())
and all other
alphabets in small
case
This function
returns the lowest str=”I am Learning Python”
index() 2
index of substring print(str.index(‘a’))
in a string.
This function
returns True if it’s
made of
str=”I am Learning Python”
alphanumeric
print(str.isalnum()) False (#it contain spaces)
characters
isalnum()
only.
str=”IamLearningPython” True
If there is one
print(str.isalnum())
space in a string,
this function will
return False.
str=”12345678″
This function True(#only digits)
print(str.isdigit())
returns True if all
isdigit() the characters in
the string are digits
str=”12345678A”
otherwise False. False(#contain alphabet)
print(str.isdigit())
This function
converts the given
string in title str=”i am learning python”
istitle() I Am Learning Python
case(first alphabet print(str.title())
of each word is in
upper case).
This function
converts
str=”I am Learning Python”
swapcase() the lowercase i AM lEARNING pYTHON
print(str.swapcase())
characters to upper
case and vice-versa.
Practice Questions
1. Write a program to count the frequency of a character in a string.
str=input("Enter any String")
ch=input("Enter the character")
print(str.count(ch))
Q2. Write a program to accept a string and return a string having first letter of each word in capital.
str=input("Enter any String")
print(str.title())
24) a=7 12
b=5 13
while(a<9):
print(a+b)
a+=1
25) b=5 H
H
while(b<9): H
print("H") H
b+=1
26) x=15 Hello
while(x==15):
print("Hello")
x=x-3
27) x = "123" a
for i in x: a
print("a") a
28) i=2 3
for x in range(i): 3
i+=1 4
print(i) 4
print(i)
29) i=2 1
2
for x in range(i): 2
x+=1
print(x)
print(x)
30) i=2 1
x
for x in range(i): 2
x+=1 x
print(x)
print("x")
31) a=7 Error
for i in 7:
print(a)
32) a = "AMIT" AMIT
for i in range(len(a)): AMIT
print(a) AMIT
AMIT
33) for i in range(0,2,-1): No output
print("Hello")