Input a name and print the initial along with title in Python
String Methods - Question 5
In this question, we will see how to input a name and print the initial first and then the title in Python programming. To know more about string methods click on the string methods lesson.
Q5) Write a program in Python to input a name and print the initial first and then the title.
Example: Input -> MOHIT KUMAR AGARWAL Output -> M.K. AGARWAL
Program
p=0
str=input("Enter a name\n")
p=str.rfind(' ') # finding the last space position
print('The initials and the title is')
for i in range(0,p):
if i==0 and str[i]!=' ':
print(str[i]+".",end='')
elif str[i]==' ' and str[i+1]!=' ':
print(str[i+1]+".",end='')
# printing the title from the last space onwards
print(str[p:len(str)])
Output
Enter a name Mohandas Karamchand Gandhi The initials and the title is M.K. Gandhi