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

Strings

Strings in Python

Uploaded by

imjoem0ma
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)
30 views

Strings

Strings in Python

Uploaded by

imjoem0ma
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/ 8

Strings

Introduction :
As we know that a sequence of characters enclosed in single quotes, double
quotes or triple quotes (‘ ‘ , “ “ , ‘’’ ‘’’) is called a string.
• In python, strings are immutable meaning they can’t be changed.
• In a String, each character remains at a unique position number or index
number which goes from 0 to n-1 (n is the total number of characters in the
string).
• String can be created in following ways:
1. By assigning value directly to the variable
2. By taking Input String Literal Input ( ) always return input in string form.
• Process to access each and every character of a string for the purpose of
display or for some other purpose is called string traversal.
# A String Slicing
#Look at following examples carefullyword = “RESPONSIBILITY” word[ 0 : 14 ]
will result into‘RESPONSIBILITY’
word[ 0 : 3] will result into‘RES’
word[ 2 : 5 ] will result into‘SPO’
word[ -7 : -3 ] will result into‘IBIL’ word[ : 14 ] will result into‘RESPONSIBILITY’
word[ : 5 ] will result into ‘RESPO’ word[ 3 : ] will result into ‘PONSIBILITY’
#STRINGS INBUILT FUNCTIONS
#1. capitalize(): returns the string
#with first letter capital
'''
a="apple"
print(a.capitalize())
#take input from user now:

1
a=input("Enter any string")
print(a.capitalize())

#2. replace(): replaces all occurences


#of the old string with new string

a="apple is red"
print(a.replace(" ","-"))

#Do this question again (take input


#from user)

#3.index(): returns the index of


#the substring
a="hello world"
print(a.index("world"))
print(a.index("hello"))
print(a.index("e"))
print(a.index(" "))
print(a.index("bi"))
print(a.index("z"))

4. isalpha(): check the string


contains only alphabet or not?(returns
true if ok else false)

2
program:
a="working is bad"
if(a.isalpha()):
print("strings contains only alphabet")
else:
print("not")
'''
#isalnum(): returns true f all characters
#are alphanumeric (a-z) and (0-9)
ex:
abc-valid
23-valid
abc23-valid
%t-invaluid
$=invalid
" "-invalid
a="ram2"
print(a.isalnum())
a="save money"
print(a[:3])
print(a[:])
print(a[-2:])
print(a[:-2])
print(a[3:7])
#STRINGS INBUILT FUNCTIONS:

3
'''
1. len():
a="apple"
b=len(a)
print(b)

2. capitalize(): this method/function


syntax:
a.capitalize()

ex:
a="kalash"
print(a.capitalize())

output:
Kalash
'''

3. replace(): this method/function


replaces all the occurences of old string
with new string
syntax: a.replace(old,new)

a="this is old string"


print("a.replace("is","was"))
output: this was old string

4
4. find(): used to return the indexz of
the substring.
syntax: a.find(sub_string,start,end)

a="green world"
p= a.find('green')
print(p)
output: 0
a="save money"
a[1:3]
'av'
a="apple:
SyntaxError: unterminated string literal (detected at line 1)
a="apple"
a[2:4]
'pl'
a="mango"
a[3:4]
'g'
a="grapes"
a[2:6]
'apes'
a="grapes"
a[3:6]
'pes'

5
a="mango is yellow"
a[3:8]
'go is'
a="apple is red1"
a[5:10]
' is r'
a="hello keshav"
a[2:20]
'llo keshav'
a="hello world"
a[1:2]
'e'
a="hello"
a[3:3]
''
a="hello world"
a[9:9]
''
>>> a="lavanya"
>>> a[20:20]
''
>>> a="mango is yellow"
>>> a[0:0]
''
>>> a="helloe"
>>> a[2:6]

6
'lloe'
>>> a="ok bi"
>>> a[1:5]
'k bi'
a="save money"
a[:3]
#wap to count no of occurence in a inputted string
'''c=0
a=input("Entera string")
b=input("Enter character")
for i in a:
if i==b:
c+=1
print(c)

'''
# wap to calculate length of a string?

a=input("enter a string")
b=len(a)
print("length of the string",b)

PRACTICE QUESTIONS :
Q1. WAP TO TAKE STRING AND PRINT IT?
Q2. WAP TO ENTER THE STRING AND PRINT IT?
Q3. WAP TO USE \n and \t in a program?

7
Q4.WAP TO USE INDEXING(LIKE:
DISPLAY 1ST CHARACTER OF THE SRTING"
Q5. DISPLAY 3RD CHARACTER OF THE STRING
Q6. DISPLAY LAST CHARACTER OF THE STRING
Q7. DISPLAY LAST CHARACTER USING
NEGATIVE INDEXING
Q8. DISPLAY 4TH CHARACTER USING
NEGATIVE INDEXING
Q9. WAP TO FIND OUT LENGTH OF THE STRING
USING len()?
Q10. WAP TO CHECK THE GIVEN STRING CONTAINS ONLY ALPHABETHS OR
NOT?
Q11. WAP TO PRINT “VERY GOOD” IF THE STRINGS CONTAINS ALPHANUMERIC
ELSE PRINT “OK BI”?

****************************************************************

You might also like