简体   繁体   English

如何让python显示输入的第一个字母

[英]How to get python to display the first letter from input

For example, if I entered "Harry Potter" into the following code.例如,如果我在以下代码中输入"Harry Potter" What should I write in the blank spaces so that First_letter will be assigned with H , Second_letter with a , etc.我应该在空格中写什么,以便First_letter将被分配为HSecond_lettera等。

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:在我看来,初学者最容易理解的解决方案是使用list()例如:

>>> 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:您可以对str对象使用索引:

Name = input("Enter your name")

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从数组中获取完整的单词而不是python中的第一个字母 - How to get full word from array instead of first letter in python 如何获取 python 中每一行的第一个字母? - How to get first letter of each line in python? 如何获取我(如果在列表中为“输入”)检查输入的每个字母,而不仅仅是输入的第一个字母? - How do I get my (if “input” in list) to check every letter of the input and not just the first letter of input? Python 3无法获取字母输入 - Python 3 unable to get letter input 我需要从字符串中获取第一个字母,并使其成为python中其他字符串的另一个字母的索引 - I need to get the first letter from a string and make it an index to an other letter from other string in python 如何从 python 中的输入值中分别获取数字和字母? - How can I get the number and the letter separately from input value in python? 如何使用python的cmd获取首字母命令? - How to get first letter commands using python's cmd? Python-如果输入是字母或数字,则显示不同的输出 - Python - display different output if the input is a letter or a number 如何在Python中获取第一个大写字母,然后再获取每个不跟另一个大写字母的字母? - How to get the first capital letter and then each that isn't followed by another capital letter in Python? 获取python中每个2个字母的单词的第一个字母 - Get the first letter of each 2-letter word in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM