1

I am new to python. I have used c before. There we can work character by character using indexing in string. I was trying to convert the whole string to lower case, reverse it and print it with first character in upper case

first_name = input("enter your first name\n")
last_name = input("enter your last name\n")
first_name = first_name.lower()
last_name = last_name.lower()
def rev(s):
 str = ""
 for i in s:
    str = i + str
    return str
first_name = rev(first_name)
last_name = rev(last_name)
first_name[0].upper()
last_name[0].upper()
print(first_name + " " + last_name)

The output is

enter your first name

Manash

enter your last name

Sharma

m s

  • Python allows string indexing as well. See this answer for how to reverse a string using Python's string indexing: [https://stackoverflow.com/questions/931092/reverse-a-string-in-python](https://stackoverflow.com/questions/931092/reverse-a-string-in-python) – vielkind May 04 '18 at 14:46

2 Answers2

0
  • You can use [::-1] to reverse your string
  • str.capitalize or str.title to get the first letter upper case

Demo:

first_name = input("enter your first name\n")
last_name = input("enter your last name\n")
first_name = first_name[::-1].capitalize()
last_name = last_name[::-1].capitalize()
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Your mistake lies in returning immediately inside the for loop, and in not assigning the capitalization:

first_name = input("enter your first name\n")
last_name = input("enter your last name\n")
first_name = first_name.lower()
last_name = last_name.lower()
def rev(s):
    str = ""
    for i in s:
       str = i + str
    return str
first_name = rev(first_name)
last_name = rev(last_name)
first_name = first_name[0].upper() + first_name[1:]
last_name = last_name[0].upper() + last_name[1:]
print(first_name + " " + last_name)

Note that there are of course nicer, quicker, and easier ways of doing this, as shown in the other answer.

L3viathan
  • 26,748
  • 2
  • 58
  • 81