String in Python
Table of Contents
What is String in Python?
How to Create Strings in Python?
Traversing Strings in Python:
Traversing the strings in Python using for loop
Operation of Strings in Python
Inbuilt Functions of Strings in Python:
Practice Questions
What is String in Python?
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
Iterating through string using for loop:
Traversing the strings in Python using for
loop
Strings in Python
Operation of Strings in Python
Strings in
Python
Membership Operator : there are two membership
operators:
1. ‘in’ operator returns True if a substring
is present in main string OUTPUT
2. ‘not in’ operator returns True if a substring
is not present in main string. True
True
str1 = “Welcome” False
print(‘com’ in str1)
print(‘W’ in str1)
print(‘Wel’ not in str1)
String Comparison : We can compare
OUTPUT
the string using relational/comparison
operator like (>, <, >=, <=, ==, !=)
False
print(“amit” == “Amit”)
True
print(“amit” != “Amit”)
False
print(“blog” > “z”)
Strings in Python
Strings in Python
NOTE: Strings are immutable means that the content of the string cannot
be changed
We can not change the character in the String
Inbuilt Functions of Strings in Python:
Function
Description Code Output
Name
This function splits
the string into a list str = ” I am Learning [‘I’ , ‘am’ ,
split() of Python” ‘Learning’ ,
string on the basis of print(str.split()) ‘Python’ ]
delimiter
This function
str = ” I am Learning I AM
converts
upper() Python” LEARNING
the string into
print(str.upper()) PYTHON
uppercase
This function str = ” I am Learning
i am learning
lower() converts the Python”
python
string into lowercase print(str.lower())
This function
replaces a
substring from the str = ” I am Learning
main Python”
I am Doing
replace() string. newstr=str.replace
Python
As strings are (“Learning”,”Doing”)
immutable print(newstr)
so this function
creates
a new string
This function return
the str = “I am Learning
find() index of substring in Python” 2
a print(str.find(‘am’))
main string.
This function returns str = “I am Learning
len() the length of the Python” 20
string. print(len(str))
This function is used
str=” I am Learning
to
Python”
remove leading 25
strip() print(len(str))
and trailing 20
strnew=str.strip()
whitespace
print(len(strnew))
from the string.
This function counts
str=”I am Learning
the number of
Python” 2
count() occurrences of a
print(str.count(‘a’)) 3
substring
print(str.count(‘n’))
in main string
This function
converts
the first alphabet str=”I am Learning
I am learning
capitalize() of the string to upper Python”
python
case and all other print(str.capitalize())
alphabets in small
case
This function returns
str=”I am Learning
the lowest index
index() Python” 2
of substring in a
print(str.index(‘a’))
string.
This function returns
True if it’s made of str=”I am Learning
alphanumeric Python” False (#it contain
characters print(str.isalnum()) spaces)
isalnum()
only. If there is one
space in a string, str=”IamLearningPython” True
this function will print(str.isalnum())
return False.
True(#only
str=”12345678″
This function returns digits)
print(str.isdigit())
True if all the
isdigit() characters
in the string are
str=”12345678A”
digits False(#contain
print(str.isdigit())
otherwise False. alphabet)
islower() This function returns str=”I am Learning False(#contain
True if all the Python” upper case
characters print(str.islower()) alphabet)
in the string are in
lower case
str=”i am learning python”
print(str.islower())
True
str=”I am Learning
False(#contain
This function returns Python”
small case)
True if all the print(str.isupper())
isupper() characters in the
string are in upper str=”I AM LEARNING
case PYTHON”
True
print(str.isupper())
str=”I am Learning False
Python”
print(str.isspace())
This function returns
True if all the
isspace() str=” “ True (#contain
characters in the
print(str.isspace()) only space)
string is whitespace.
str = “”
print(str.isspace()) False
This function
converts
the given string in
title case(first str=”i am learning python” I Am Learning
istitle()
alphabet of each print(str.title()) Python
word is in upper
case).
This function
converts str=”I am Learning
i AM lEARNING
swapcase() the lowercase Python”
pYTHON
characters to upper print(str.swapcase())
case and vice-versa.
Functions of Strings in Python
Practice Questions
Q1. 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())
Q3. Write a program to accept a string and display the following:
1. Number of uppercase characters
2. Numbers of lowercase characters
3. Total number of alphabets
4. Number of digits
str=input("Enter any String")
u=0
L=0
d=0
l = len(str)
for i in range(l):
if str[i].isupper():
u = u+1
if str[i].islower():
L=L+1
if str[i].isdigit():
d = d+1
print("Total Upper Case Characters are: ", u)
print("Total Lower Case Characters are: ", L)
print("Total Characters are: ", L + u)
print("Total digits are: ", d)
Q4. Write a program to reverse a string.
str=input("Enter any String")
print(str[::-1])