In: Computer Science
<Python coding question string practice>
Can anyone please answer these python string questions??
1. Write a function called username that takes as parameters a person's first and last names and prints out his or her username formatted as the last name followed by an underscore and the first initial. For example, username("Martin", "freeman") should print the string "freeman_m".
2. Write a function called letters that takes as a parameter a string and prints out the characters of the string, one per line. For example letters("abc") should print
a
b
c
3. Write a function called backward that takes as a parameter a string and prints out the characters of the string in reverse order, one per line. For example backward("abc") should print
c
b
a
4. Write a function called noVowels that takes as a parameter a string and returns the string with all the vowels removed. For example, noVowels("this is an example.") should return "the s n xmpl.".
Here is the completed code for this problem including code for testing. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#method taking first and last names and prints username
formatted
# as the last name followed by an underscore and the first
initial
def username(first, last):
#concatenating lastname and underscore and
first letter of first name and
#converting the resultant string to lower case
& printing it
print((last+'_'+first[0]).lower())
#method to print characters in a text one by one
def letters(text):
#simply looping through text and printing
each character
for c in
text:
print(c)
#method to print characters in a text one by one in
reverse
def backward(text):
#looping from i=0 to i=len(text)-1
for i in
range(len(text)):
#printing ith
character from back
print(text[len(text)-1-i])
#method to return a string with all the vowels removed from
parameter string
def noVowels(text):
result=''
for c
in text:
#converting c to
lowercase and if it is not in the string aeiou, appending to
result
if
c.lower() not in 'aeiou':
result+=c
#returning result
return result
#testing all methods
#getting first and last name, testing username method
first=input("Enter first name: ")
last=input("Enter last name: ")
username(first,last)
#reading another text
text=input("Enter some text: ")
#testing letters method
print("Printing letters forward")
letters(text)
#testing backward method, using same text
print("Printing letters backward")
backward(text)
#testing noVowels method, using same text
print("Removing vowels from text")
print(noVowels(text))
#output