简体   繁体   中英

How to get python to display the first letter from input

For example, if I entered "Harry Potter" into the following code. What should I write in the blank spaces so that First_letter will be assigned with H , Second_letter with a , etc.

If possible, please also explain how the code works.

Any help will be greatly appreciated!

Name = input("Enter your name")

First_letter = ____
Second_letter = ____
Third_letter = ____

The easiest solution for beginners to understand in my opinion is using list() like:

>>> name = list(input("Enter your name"))
>>> name
['n', 'a', 'm', 'e']
>>> First_letter = name[0]
>>> Second_letter = name[1]
>>> Third_letter = name[2]

You could use index for str objects:

Name = input("Enter your name")

First_letter = Name[0]
Second_letter = Name[1]
Third_letter = Name[2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM