0% found this document useful (0 votes)
3 views4 pages

Python String Basics and Functions

Class 12 computerr notes
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)
3 views4 pages

Python String Basics and Functions

Class 12 computerr notes
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

REVISION TOUR STRING

 Combination of characters, number , special character enclosed in single, double or triple quotation
marks.
 Strings are immutable in python. It means it is unchangeable.

EXAMPLE
Forward indexing

0 1 2 3 4 5 6 7
I n d i a @ 7 5
-8 -7 -6 -5 -4 -3 -2 -1

Backword Indexing

s="India@75" OUTPUT
print(s[2]) d

s="India@75" OUTPUT
print(s[-2]) 7

s="India@75" OUTPUT
print(s[2:6]) dia@

s="India@75" OUTPUT
print(s[0:7:2]) Ida7

s="India@75" OUTPUT
print(s[2:]) dia@75

s="India@75" OUTPUT
print(s[:5]) India

s="India@75" OUTPUT
print(s[-5:-1]) ia@7

s="India@75" OUTPUT
print(s[: : -1]) 57@aidnI
s1="GREAT" OUTPUT
s2="INDIA" GREATINDIA
print(s1+s2)

s="INDIA" OUTPUT
print(s*2) INDIAINDIA

STRING RELATED FUNCTIONS


FUNCTION EXAMPLE OUTPUT

len( ) Returns the length of a string 5


s="INDIA"
print(len(s))

capitalize( ) Returns a string with its first character capitalized. India is great
s="india is great"
print([Link]())

title() Returns a string with its first character of every word India Is Great
capitalized.
S ="india is great"
print([Link]())

isalpha( ) Returns True if all characters in the string are alphabetic. True
False otherwise.
s="india"
print([Link]())

isdigit() Returns True if all the characters in the string are digits. True
False otherwise
s="26041975"
print([Link]())

isalnum( ) Returns True if the characters in the string are alphabets or True
numbers. False otherwise

s="India1975"

print([Link]())
islower( ) Returns True if all the characters in the string are digits. True
False otherwise
s="india@1975"
print([Link]())

isupper( ) Returns True if all the characters in the string are True
uppercase. False otherwise
s="INDIA@1975"
print([Link]())

isspace( ) Returns True if there are only whitespace characters in the True
string. False otherwise.
s=" "
print([Link]())

lower( ) Converts a string in lowercase characters. india@75


s="InDIa@75"
print([Link]())

upper( ) Converts a string in uppercase characters. INDIA@75


s="India@75"
print([Link]())

split( ) breaks a string into words and creates a list out of it ['India', 'is', 'great']
s="India is great"
print([Link]())

partition ( ) breaks a string into three parts and creates a tuple. ('India’, 'is’, ‘great')
s="India is great"
s. [Link]('is')

join( ) It separate every character by given string. 'i_n_d_i_a'


s='india'
'_'.join(s)

startswith() Returns True when string starts with particular value True
s="India is great"
print([Link]("Ind"))
endswith() Returns True when string ends with particular value True
s="India is great"
print([Link]("eat"))
Define count(s) to count number of character , digits and special character present in a
string

def count(s):

a=d=c=0

for z in s:

if [Link]():

a=a+1

if [Link]():

d=d+1

if not [Link]():

c=c+1

print("Alphabets" , a)

print("Digits=" , d)

print("Special Character=" , c)

s="India@1975@"

count(s)

OUTPUT

Alphabets 5

Digits= 4

Special Character= 2

You might also like