0% found this document useful (0 votes)
23 views

String Revision

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

String Revision

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CLASS XII

COMPUTER SCIENCE
STRINGS IN PYTHON
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 OUTPUT


operators:
1. ‘in’ operator returns True if a substring True
is present in main string True
2. ‘not in’ operator returns True if a substring False
is not present in main string.

str1 = “Welcome”
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 Python” [‘I’ , ‘am’ , ‘Learning’ ,
split() of
print(str.split()) ‘Python’ ]
string on the basis of
delimiter

This function
converts str = ” I am Learning Python” I AM LEARNING
upper()
the string into print(str.upper()) PYTHON
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
str = ” I am Learning Python”
main
newstr=str.replace
replace() string. I am Doing Python
(“Learning”,”Doing”)
As strings are
print(newstr)
immutable
so this function
creates
a new string

This function return


the
str = “I am Learning Python”
find() index of substring in 2
print(str.find(‘am’))
a
main string.

This function
str = “I am Learning Python”
len() returns the length of 20
print(len(str))
the string.

This function is used


to str=” I am Learning Python”
remove leading print(len(str)) 25
strip()
and trailing strnew=str.strip() 20
whitespace print(len(strnew))
from the string.

This function counts


the number of str=”I am Learning 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
of the string to str=”I am Learning Python”
capitalize() I am learning python
upper print(str.capitalize())
case and all other
alphabets in small
case

This function
returns
str=”I am Learning Python”
index() the lowest index 2
print(str.index(‘a’))
of substring 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)
isalnum() characters
only. If there is one str=”IamLearningPython” True
space in a string, print(str.isalnum())
this function will
return False.

This function str=”12345678″


True(#only digits)
returns print(str.isdigit())
True if all the
isdigit()
characters
in the string are str=”12345678A”
False(#contain alphabet)
digits print(str.isdigit())
otherwise False.

False(#contain upper case


str=”I am Learning Python”
This function alphabet)
print(str.islower())
returns
True if all the
islower()
characters
in the string are in
str=”i am learning python”
lower case
print(str.islower())
True

This function str=”I am Learning Python”


False(#contain small case)
returns print(str.isupper())
True if all the
isupper()
characters in the str=”I AM LEARNING
string are in upper PYTHON”
True
case print(str.isupper())
str=”I am Learning Python” False
print(str.isspace())
This function
returns
str=” “
isspace() True if all the
print(str.isspace()) True (#contain only space)
characters in the
string is whitespace.
str = “”
print(str.isspace()) False

This function
converts
the given string in
title case(first str=”i am learning python”
istitle() I Am Learning Python
alphabet of each print(str.title())
word is in upper
case).

This function
converts
str=”I am Learning Python” i AM lEARNING
swapcase() the lowercase
print(str.swapcase()) pYTHON
characters to upper
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])

You might also like