Top Python Program of String for beginner
Top Python Program of String for beginner is written below,
1 Write a Python program to calculate the length of a string.
s=input("enter string= ") print("length of your string is ", len(s))
Output:
enter string= I am Python lover
length of your string is 17
2 Write a Python program to get a string made of the first 2 and the last 2 chars from a given a string. Ex Input : beautiful Expected Output : beul
s=input("Enter string= ") f=s[0]+s[1] l=s[-2]+s[-1] print("string made of the first 2 and the last 2 chars from your string is ", f+l)
Output:
Enter string= python
string made of the first 2 and the last 2 chars from your string is pyon
3 Write a Python program to get a string from a given string where all occurrences of its first char have been changed to ‘$’, except the first char itself.
Ex Input : abracadabra Expected Output : abr$c$d$br$
s=input("Enter string= ") s1=s s2=s[1:] for i in s2: if i==s[0]: s2= s2.replace(i,"$") print(s1[0],end="") print(s2)
Output:
Enter string= sgjfderiodskifsjhsbsasskwsqsxxxssssn
sgjfderiod$kif$jh$b$a$$kw$q$xxx$$$$n
4 Write a Python program to get a single string from two given strings, separated by a space and swap the first two characters of each string.
Ex Input : st1=hello st2=world
Expected Output : st3=wollo herld
s1=input("Enter a String: ") s2=input("Enter another String: ") s4 = s1[0:2].replace(s1[0:2], s2[0:2]) s5 = s1[0:2].replace(s2[0:2], s1[0:2]) print("output: ",s4+s1[2:],s5+s2[2:])
Output:
Enter a String: python
Enter another String: world
output: wothon pyrld
5 Write a Python program to add ‘ing’ at the end of a given string (length should be at least 3). If the given string already ends with ‘ing’ then add ‘ly’ instead. If the string length of the given string is less than 3, leave it unchanged.
Ex Input : test Expected Output : testing
If Input : testing Expected Output: testingly
s1=input("Enter string : ") length=len(s1) index=length-3 if length>=3: if s1[index:]=="ing": s1=s1+"ly" else: s1=s1+"ing" else: pass print(s1)
Output:
Enter string : test
testing
or
Enter string : programming
programmingly
6 Write a Python program to remove the nth index character from a nonempty string.
s1=input("Enter string : ")
length=len(s1)
index=length-1
if index>1:
print("Remove nth Index from given String")
print(s1[:index])
else:
print("Please Enter Valid String {Minimum 2 Char }")
Output:
Enter string : python
Remove nth Index from given String
pytho
7 Write a Python program to remove the characters which have odd index values of a given string.
s1=input("Enter string : ")
print(s1[1::2])
Output:
Enter string : python
yhn
8 Write a Python script that takes input from the user in proper cause and displays that input back in upper and lower cases.
s1=input("Enter string : ")
if s1.isupper()==True:
print("In LowerCase")
print(s1.lower())
else:
print("In UpperCase")
print(s1.upper())
Output:
Enter string : python
In UpperCase
PYTHON
I Think You Understand above Topic-Top Python Program of String for Beginner.
For any query feel free to fill comment section and send it.
More Topic of Python:
Python Program of Basic Question’s Part-1
Python Program of Basic Question’s Part-2
Mind Blowing While Loop program for beginner
Recent Comments