In: Computer Science
Write a Python program that has the user enter their name. Using the user's name calculate the following:
1. How many letters are in the user's name?
2. Print the user's name in REVERSE both in capital letters and lowercase letters
3. Print the ASCII value for each letter of the user's name.
4. Print the SUM of all of the letters of the user's name (add each letter from #3)
1.
The line of code from which the length of the user name is computed in python is provided below:
#Compute the length of the entered user name.
print('Length of the user name : ' , len(user_name))
2.
The line of code to reverse the upper case and the lower case letters is provided below:
#Convert the user name in lower and the upper case.
username_lower = user_name.lower()
username_upper = user_name.upper()
#Display the reversed string in upper and lower both case.
print('Reversed letter in lower case :', username_lower[::-1])
print('Reversed letter in upper case :', username_upper[::-1])
3.
The line that display the ASCII values of the user’s name is provided below:
#Display the ASCII values of the entered user name.
for i in user_name:
print('ASCII value of ', i ,':' ,ord(i))
4.
The line to sum up the letters of the user’s name is provided below:
#Display the sum of the letters of the entered user name.
print('Sum of letters of the user name : ',sum(map(ord, user_name)))
The complete code of the above four parts in python is provided below:
Screenshot of the code:
Sample Output:
Code To Copy:
#Run the code in python version 3.x.
#Indent the code as per the above screenshot of the code. Otherwise it may give error.
#Prompt the user to enter the user's name.
user_name = input('Enter the user name : ')
#SOLUTION FOR PART 1.
#Compute the length of the entered user name.
print('Length of the user name : ' , len(user_name))
#SOLUTION FOR PART 2.
#Convert the user name in lower and the upper case.
username_lower = user_name.lower()
username_upper = user_name.upper()
#Display the reversed string in upper and lower both case.
print('Reversed letter in lower case :', username_lower[::-1])
print('Reversed letter in upper case :', username_upper[::-1])
#SOLUTION FOR PART 3.
#Display the ASCII values of the entered user name.
for i in user_name:
print('ASCII value of ', i ,':' ,ord(i))
#SOLUTION FOR PART 4.
#Display the sum of the letters of the entered user name.
print('Sum of letters of the user name : ',sum(map(ord, user_name)))