Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Dev-lab-4

AMAN LAB 4
March 18, 2024

[ ]: Dev JAIN(231031160010)

LAB-4(STRING)
PROGRAM-1: WAP to input three strings from a user using only one input() command amd print
the concatenated string

[7]: a,b,c= str(input("Enter any three string:")).split()


print(a+b+c)

Enter any three string: Hello Gurugram University


HelloGurugramUniversity
PROGRAM-2: WAP to to reverse the string using 1) string slicing and 2) reverse() function

[13]: #using slicing method


string= "Hello Gurugram University"[::-1]
print("reverse string using slicing method is:",string)

#using reverse() function


def reverse(string):
string1="".join(reversed(string))
return string1
input="Hello Gurugram University"
print("reverse string using reverse() function is:",reverse(input))

reverse string using slicing method is: ytisrevinU marguruG olleH


reverse string using reverse() function is: ytisrevinU marguruG olleH
#PROGRAM 3: WAP to demostrate various ways of accessing the string. 1) by using index-
ing(positive and negative) 2) by using slice operater

[27]: string= "Hello Gurugram University"


#using positive indexing
print(string[0],string[1],string[2],string[3],string[4])
#using negetive indexing print(string[-10],string[-
9],string[-8])
#using slice operator
print(string[:5])

1
print(string[6:15])
print(string[-10:-1])

H e l l o
U n i Hello
Gurugram
Universit
PROGRAM 4: WAP to demonstrate the following function/methods
which operates on strings in python with suitable example :
len(),strip(),rstrip(),lstrip(),find(),index(),count(),replace(),split(),upper(),lower(),swapcase(),title(),capitalize(),startswith(),

[56]: #len function


input1 = "hello world"
print('1-',len(input1))
#strip() function
input2 = " hello world "
print('2-',input2.strip())
#rstrip function
input3 = "hello world@@"
print('3-',input3.rstrip('@'))
#lstrip function
input4= "##hello world"
print('4-',input4.lstrip('#'))
#find function
input5= " hello world"
print('5-',input5.find("w"))
#index function
input6= " hello world"
print('6-',input6.index("d"))
#count function
input7= "hello world"
print('7-',input7.count('l'))
#replace function
input8= "hello world" print('8-
',input8.replace("world","gurugram")) #split
function
input9= "hello world"
print('9-',input9.split())
#upper function
input10= "hello world"
print('10-',input10.upper())
#lower function
input11 = "HELLO WORLD"
print('11-',input11.lower())
#swapcase function
input12= "hello WORLD"

2
print('12-',input12.swapcase())
#TITLE function
input13= "hello world"
print('13-',input13.title())
#capitalize function
input14="hello world"
print('14-',input14.capitalize())
#startswith function
input15="hello world" print('15-
',input15.startswith('k')) print('15-
',input15.startswith('h')) #endswith
function
input16="hello world" print('16-
',input16.endswith('f')) print('16-
',input16.endswith('world'))

1- 11
2- hello world
3- hello world
4- hello world
5- 7
6- 12
7- 3
8- hello gurugram
9- ['hello', 'world'] 10-
HELLO WORLD 11-
hello world
12- HELLO world
13- Hello World
14- Hello world
15- False
15- True
16- False
16- True

#PROGRAM 5: WAP to find the length of a string using 1)inbuilt function len() and 2) for loop

[64]: #find length by len function


string="Hello Gurugram University"
print("length of string by using function isd:",len(string))
#find length by for loop
def length(string):
count=0
for i in string:
count +=1
return count
print("length of string by for loop is:",length(string))

3
length of string by using function isd: 25
length of string by for loop is: 25

[ ]:

You might also like