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

Strings 11

Uploaded by

pkhatri0701
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)
7 views

Strings 11

Uploaded by

pkhatri0701
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/ 14

Made with Xodo PDF Reader and Editor

New
syllabus
Chapter 11 2023-24

Strings

Computer Science
Class XI ( As per CBSE Board)

Visit : python.mykvs.in for regular updates


Made with Xodo PDF Reader and Editor

String

String is a sequence of characters,which is enclosed


between either single (' ') or double quotes (" "),
python treats both single and double quotes same.

Visit : python.mykvs.in for regular updates


Made with Xodo PDF Reader and Editor

String

Creating String
Creation of string in python is very easy.
e.g.
a=‘Computer Science'
b=“Informatics Practices“
Accessing String Elements
e.g.
('str-', 'Computer Sciene')
str='Computer Sciene' ('str[0]-', 'C')
print('str-', str) ('str[1:4]-', 'omp')
print('str[0]-', str[0]) ('str[2:]-', 'mputer Sciene')
print('str[1:4]-', str[1:4]) ('str *2-', 'Computer
print('str[2:]-', str[2:]) ScieneComputer Sciene')
print('str *2-', str *2 ) OUTPUT ("str +'yes'-", 'Computer
print("str +'yes'-", str +'yes') Scieneyes')

Visit : python.mykvs.in for regular updates


Made with Xodo PDF Reader and Editor

String

Iterating/Traversing through string


Each character of the string can be accessed sequentially using for
C
loop. o
m
p
e.g. u
t
e
r
str='Computer Sciene‘ OUTPUT
S
for i in str:
c
print(i) i
e
n
e
Visit : python.mykvs.in for regular updates
Made with Xodo PDF Reader and Editor

String
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 .
e.g.program
OUTPUT
print("Maria" == "Manoj") False
print("Maria" != "Manoj") True
print("Maria" > "Manoj") True
print("Maria" >= "Manoj") True
print("Maria" < "Manoj") False
print("Maria" <= "Manoj") False
print("Maria" > "") True

Visit : python.mykvs.in for regular updates


Made with Xodo PDF Reader and Editor

String
Updating Strings
String value can be updated by reassigning another value in
it.
e.g.
var1 = 'Comp Sc'
var1 = var1[:7] + ' with Python'
print ("Updated String :- ",var1 )

OUTPUT
('Updated String :- ', 'Comp Sc with Python')

Visit : python.mykvs.in for regular updates


Made with Xodo PDF Reader and Editor

String
String Special Operators
e.g.
a=“comp”
B=“sc”
Operator Description Example
+ Concatenation – to add two strings a + b = comp sc
* Replicate same string multiple times (Repetition) a*2 = compcomp
[] Character of the string a[1] will give o
[:] Range Slice –Range string a[1:4] will give omp
in Membership check p in a will give 1
not in Membership check for non availability M not in a will give 1
% Format the string

print ("My Subject is %s and class is %d" % ('Comp Sc', 11))

Visit : python.mykvs.in for regular updates


Made with Xodo PDF Reader and Editor

String

Format Symbol

%s -string conversion via str() prior to formatting


%i -signed decimal integer
%d -signed decimal integer
%u -unsigned decimal integer
%o -octal integer
%x -hexadecimal integer (lowercase letters)
%X -hexadecimal integer (UPPERcase letters)
%e -exponential notation (with lowercase 'e')
%E -exponential notation (with UPPERcase 'E')
%f -floating point real number
%c -character
%G -the shorter of %f and %E
Visit : python.mykvs.in for regular updates
Made with Xodo PDF Reader and Editor

String

Triple Quotes
It is used to create string with multiple lines.

e.g.
Str1 = “””This course will introduce the learner to text
mining and text manipulation basics. The course begins
with an understanding of how text is handled by python”””

Visit : python.mykvs.in for regular updates


Made with Xodo PDF Reader and Editor

String
String functions and methods
a=“comp”
b=“my comp”
Method Result Example
len() Returns the length of the string r=len(a) will be 4
str.capitalize() To capitalize the string r=a.capitalize() will be “COMP”

str.title() Will return title case string


str.upper() Will return string in upper case r=a.upper() will be “COMP”
str.lower() Will return string in lower case r=a.upper() will be “comp”
will return the total count of a
str.count() r=a.count(‘o’) will be 1
given element in a string
To find the substring position(starts r=a.find (‘m’) will be 2
str.find(sub)
from 0 index)
Return the string with replaced sub r=b.replace(‘my’,’your’) will be ‘your
str.replace()
strings comp’

Visit : python.mykvs.in for regular updates


Made with Xodo PDF Reader and Editor

String
String functions and methods
a=“comp”
Method Result Example
r=a.index(‘om’) will
str.index() Returns index position of substring
be 1
String consists of only alphanumeric characters (no r=a.isalnum() will
str.isalnum()
symbols) return True
str.isalpha() String consists of only alphabetic characters (no symbols)
str.islower() String’s alphabetic characters are all lower case
str.isnumeric() String consists of only numeric characters
str.isspace() String consists of only whitespace characters
str.istitle() String is in title case
str.isupper() String’s alphabetic characters are all upper case

Visit : python.mykvs.in for regular updates


Made with Xodo PDF Reader and Editor

String
String functions and methods
a=“comp”
Method Result Example

b=‘**comp’;
str.lstrip(char) Returns a copy of the string with leading/trailing
r=b.lstrin() will be
str.rstrip(char) characters removed
‘comp’

Removes specific character from leading and trailing


str.strip(char)
position

b=‘my comp’;
r=b.split() will be
str.split() Returns list of strings as splitted
[‘my’,‘comp’]

b=‘my comp’;
r=b.partition(‘comp’
str.partition() Partition the string on first occurrence of substring
) will be
[‘my’,‘comp’]

Visit : python.mykvs.in for regular updates


Made with Xodo PDF Reader and Editor

String
#Python Program to calculate the number of digits and
letters in a string

string=raw_input("Enter string:")
count1=0
count2=0
for i in string:
if(i.isdigit()):
count1=count1+1
count2=count2+1
print("The number of digits is:")
print(count1)
print("The number of characters is:")
print(count2)

Visit : python.mykvs.in for regular updates


Made with Xodo PDF Reader and Editor

String
Searching for Substrings
E.g. program
METHOD NAME METHODS DESCRIPTION:
s = "welcome to python"
endswith(s1: str): bool Returns True if strings ends
with substring s1
print(s.endswith("thon"))
print(s.startswith("good"))
startswith(s1: str): bool Returns True if strings starts
with substring s1
print(s.find("come"))
print(s.find("become"))
count(substring): int Returns number of
occurrences of substring the
print(s.rfind("o"))
string print(s.count("o"))
find(s1): int Returns lowest index from
where s1 starts in the string, OUTPUT
if string not found returns -1 True
False
rfind(s1): int Returns highest index from 3
where s1 starts in the string, -1
if string not found returns -1
15
3

Visit : python.mykvs.in for regular updates

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