0% found this document useful (0 votes)
19 views6 pages

STRING - NOTES - For (11th Cbse)

The document provides an overview of strings in Python, explaining their definition, creation, and traversal methods. It covers string operations, including membership, comparison, and various built-in functions for manipulating strings. Additionally, it includes practice questions to reinforce understanding of string concepts.
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)
19 views6 pages

STRING - NOTES - For (11th Cbse)

The document provides an overview of strings in Python, explaining their definition, creation, and traversal methods. It covers string operations, including membership, comparison, and various built-in functions for manipulating strings. Additionally, it includes practice questions to reinforce understanding of string concepts.
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/ 6

ST JOSEPH’S SCHOOL

SUBJECT – COMPUTER SCIENCE


STRINGS
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 String?
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”
Traversing :
Traversing a String: It means accessing all the elements of the string one by one using
index value.
St = “PYTHON”

St[1] = Y St[5] = N St[-1] = N St[-6] = P

Iterating through string using for loop:


Traversing the using for loop

Operation of String

Membership Operator : 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. OUTPUT
str1 = “Welcome”
print(‘com’ in str1) True
print(‘W’ in str1) True
print(‘Wel’ not in str1) False
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”)

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 :

Function
Description Code Output
Name

This function splits


[‘I’ , ‘am’ ,
the string into a list of str = ” I am Learning Python”
split() ‘Learning’ ,
string on the basis of print(str.split())
‘Python’ ]
delimiter

I AM
This function converts str = ” I am Learning Python”
upper() LEARNING
the string into uppercase print(str.upper())
PYTHON
This function converts the str = ” I am Learning Python” i am learning
lower()
string into lowercase print(str.lower()) python

This function replaces a


str = ” I am Learning Python”
substring from the main
newstr=str.replace I am Doing
replace() string.
(“Learning”,”Doing”) Python
As strings are immutable
print(newstr)
so this function creates
a new string

This function return the


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

This function returns the str = “I am Learning Python”


len() 20
length of the string. print(len(str))

This function is used to str=” I am Learning Python”


remove leading print(len(str)) 25
strip()
and trailing whitespace strnew=str.strip() 20
from the string. print(len(strnew))

This function counts


str=”I am Learning Python”
the number of 2
count() print(str.count(‘a’))
occurrences of a substring 3
print(str.count(‘n’))
in main string

This function converts


the first alphabet
str=”I am Learning Python” I am learning
capitalize() of the string to upper
print(str.capitalize()) python
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”
False (#it contain
alphanumeric 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.
str=”12345678″ True(#only digits)
This function returns print(str.isdigit())
True if all the characters
isdigit()
in the string are digits
otherwise False. str=”12345678A” False(#contain
print(str.isdigit()) alphabet)

islower() This function returns str=”I am Learning Python” False(#contain


True if all the characters print(str.islower()) upper case
in the string are in lower alphabet)
case str=”i am learning python”
print(str.islower()) True

str=”I am Learning Python” False(#contain


This function returns print(str.isupper()) small case)
True if all the
isupper()
characters in the str=”I AM LEARNING
string are in upper case PYTHON”
print(str.isupper()) True

str=”I am Learning Python” False


print(str.isspace())
This function returns
True if all the str=” “ True (#contain
isspace()
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


the lowercase str=”I am Learning Python” i AM lEARNING
swapcase()
characters to upper print(str.swapcase()) pYTHON
case and vice-versa.

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:
Number of uppercase characters
Numbers of lowercase characters
Total number of alphabets
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy