Python - Strings Ask the user to enter a number and then print the total of the
ID: 3706171 • Letter: P
Question
Python - Strings
Ask the user to enter a number and then print the total of the individual digits. If the user enters 1234, the program would print 10 (i.e. the sum of 1+2+3+4)
Write a program to count the number of times the letter E (both uppercase and lowercase) appears in a string that the user provides.
Write a user-defined function called gen_login_id(first,last) that takes first and last name as parameters and generates a default user login id that takes the first three letters of first name and last three letters of last name. The function will return this generated login ID.
Please write a program to ask the user to input an email (for example: jr999@domainname.com) and then separate the userid from the email and print it. In the example above, the print output would be jr999.
Please write a program to ask the user to input an email (for example: julio.rojas@baruch.suny.edu) and then write a program to extracts two pieces of data from the email - user id and top level domain name - and then stores this data in a list. In the example above, the list will contain ['julio.rojas', 'suny'].
Explanation / Answer
//To find sum of digits
f = input("Enter a number: ") #Taking input from user
f = int(f) #Converting the input into integer as the input from console is by default as string
s = 0 #Initial sum is zero(0)
while(f>0): #Condition for checking the len of number
r = f % 10 #Finding reminder
s += r #Adding the reminder(last digit) to sum
f //= 10 #Deleting the last digit
print(s) #Printing the value
//To find the letter E
s = input() #Taking input from user
c = 0 #Initiate the count to zero(0)
for char in s: #Char is as substring od string s
if char == 'e' or char == 'E': #Checking the condition
c += 1 #Add 1 to count
print(c) #Display count
//Default user loginID
def gen_login_id(first,last): #Defying the function
return first[:3]+last[len(last)-3:] #Using string concatenation with string slicing
print(gen_login_id("Mark","Angel")) #Example to how to call function
//User name form emailid
email = input("Enter emailid: ") #Taking input from user
i = email.index('@') #Finding the index value of the @ sign
print(email[0:i]) #Slicing the string until the index value of @
//Two pieces of data from email
email = input("Enter emailid: ") #Taking input from user
i = email.index('@') #Finding the index value of the @ sign
out = list([email[0:i],email.split('.')[-2]]) #Storing the data into list format using list, email[0:1] gives the name for the emailid and email.split('.'[-2] gives top level domain name. the split function splits the given string with passed argument, here we used '.' and [-2] gives the second string from the back after splitting )
print(out) #Displaying the output
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.