Skip to content

Latest commit

 

History

History
121 lines (119 loc) · 1.84 KB

40. Unit 4 - Lesson 1.md

File metadata and controls

121 lines (119 loc) · 1.84 KB

Introduction to Strings

Q1.

x=input("str: ")
print(x)

String Operations

Q1.

str1 = "This is my first String"
# print the string
print(str1)
print(str1[11])
print(str1[-6])
# print letter 'f' using Positive Index
# print letter 'S' using Negative Index.

Q2.

str = "How are you?"
print("String is", str)
# print 'are' in String using Slicing with Positive Index
print(str[4:7])
# print 'w a' in String using Slicing with Positive Index
print(str[2:5])
# print 'you' in String using Slicing with Negative Index
print(str[-4:-1])
# print 'uoy' in the string using slicing and Negative indexes and negative step
print((str[-4:-1])[::-1])
# print 'you?' in String using Slicing with Negative Index
print(str[-4:])

Q3.

#Type Content here...
x=input("input: ")
if len(x)<3:
	print("output:",x)
	
else:
	print("output:",x[0:2]+x[-2:])

Q4.

a=input("str: ")
print("output:",a[1:-1])

Q5.

a=input("str: ")
if len(a)==1:
	print(a)
elif len(a)==0:
	print("null")
else:
	print("output:",a[-1]+a[1:-1]+a[0])

Q6.

a=input("str1: ")
b=input("str2: ")
c=a+b
d=b+a
print("result:",c+d)

Q7.

a=input("str: ")
b=int(input("num: "))
if len(a)<(b):
	print("num should be positive, less than the length of str")
else:
	print("output: "+a[0:b]+a[b+1:])

Q8.

a=input("str1: ")
b=input("str2: ")
print(a+" "+b)

Q9.

a=input("str1: ")
b=input("str2: ")
if len(a)>1 and len(b)>1:
	print("output:",a[1:]+b[1:])
else:
	print("null")

Q10. 3
Q11.

a=input("str: ")
print(a*4)
print(a[::-1]*3)

Q12.

a=input("str: ")
if len(a)>=3:
	print("result:",a[0:3]*3)
else:
	print("result:",a)

Q13.

a=input("str: ")
b=int(input("num: "))
print("result:",a*b)

Q14.

a=input("str: ")
b=int(input("num: "))
if b>0:
	print("result:",a[:b]*b)
else:
	print("num should be positive, less than length of str")

Q15. 1