Python: AttributeError: 'str'对象没有属性'1s' [duplicate]

时间:2021-10-15 08:47:19

This question already has an answer here:

这个问题已经有了答案:

I am trying to print the first letter of the inputted middle name, and print that, instead of the entire middle name

我正在尝试打印输入的中间名的第一个字母,并打印它,而不是整个中间名。

first_name = str(input("Please enter your first name: "))
middle_name = str(input("Please enter your middle name: "))
last_name = str(input("Please enter your last name: "))

first_name = first_name.capitalize()
first_name = middle_name.capitalize()
last_name = last_name.capitalize()

name_format = "{first} {middle.1s} {last}"
print(name_format.format(first=first_name, middle=middle_name, last=last_name)) 

Whenever I run the script in Cmd it states

每当我在Cmd中运行脚本时,它都会声明

AttributeError: 'str' object has no attribute '1s'. 

2 个解决方案

#1


1  

You need to slice your string middle. 1s is not a command Python understands but [0] is. See string slicing.

你需要把你的字符串切成中间。1s不是Python理解的命令,但是[0]理解。看到字符串切片。

Your working code:

你的工作代码:

first_name = str(input("Please enter your first name: "))
middle_name = str(input("Please enter your middle name: "))
last_name = str(input("Please enter your last name: "))

first_name = first_name.capitalize()
middle_name = middle_name.upper()
last_name = last_name.capitalize()

name_format = "{first} {middle[0]} {last}"
print(name_format.format(first=first_name, middle=middle_name, last=last_name)) 

Modified:

修改:

name_format = "{first} {middle[0]} {last}"

Slicing strings are versatile. For example:

分割字符串是多才多艺的。例如:

>>>a='Pythom'
>>>a[0]
P
>>>a[0:]
ythom
>>>a[0:2]
pyt
>>>a[:3]
Pyth
>>>a[-1] 
m
>>>a[-1] = 'n'
>>>a
Python

You are doing the same here. You input your name and slice at a point. Note that string slicing starts at 0 for the first character and not 1.

你也在做同样的事情。你输入你的名字并在某一点上切分。注意,字符串切片从第一个字符的0开始,而不是1。

#2


1  

.1s in a format string is the syntax from the older printf-style formatting. There, it would have looked liked this:

格式字符串中的.1s是来自旧的printf格式的语法。在那里,它看起来就像这样:

>>> '%s %.1s %s' % ('Guido', 'van', 'Rossum')
'Guido v Rossum'

With the format string syntax, which you are using, this looks a bit different. Format specifiers are to be specified after a colon, like this:

使用您正在使用的格式字符串语法,这看起来有点不同。格式说明符应该在冒号之后指定,如下所示:

>>> '{} {:.1s} {}'.format('Guido', 'van', 'Rossum')
'Guido v Rossum'

Or using named groups:

或使用命名组:

>>> '{first} {middle:.1s} {last}'.format(first='Guido', middle='van', last='Rossum')
'Guido v Rossum'

This is using the precision to limit the string length, as documented (emphasis mine):

这是使用精度来限制字符串长度,如文档(强调我的):

The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with 'f' and 'F', or before and after the decimal point for a floating point value formatted with 'g' or 'G'. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The precision is not allowed for integer values.

精度是一个十进制数,指示在以'f'和'f'格式的浮点值的小数点后显示多少位,或者在以'g'或'g'格式的浮点值的小数点前后显示多少位。对于非数字类型,字段指示最大字段大小——换句话说,将从字段内容中使用多少字符。整数值不允许使用精度。

(Yes, I am aware that “van” is not a middle name…)

(是的,我知道“van”不是中间名……)

#1


1  

You need to slice your string middle. 1s is not a command Python understands but [0] is. See string slicing.

你需要把你的字符串切成中间。1s不是Python理解的命令,但是[0]理解。看到字符串切片。

Your working code:

你的工作代码:

first_name = str(input("Please enter your first name: "))
middle_name = str(input("Please enter your middle name: "))
last_name = str(input("Please enter your last name: "))

first_name = first_name.capitalize()
middle_name = middle_name.upper()
last_name = last_name.capitalize()

name_format = "{first} {middle[0]} {last}"
print(name_format.format(first=first_name, middle=middle_name, last=last_name)) 

Modified:

修改:

name_format = "{first} {middle[0]} {last}"

Slicing strings are versatile. For example:

分割字符串是多才多艺的。例如:

>>>a='Pythom'
>>>a[0]
P
>>>a[0:]
ythom
>>>a[0:2]
pyt
>>>a[:3]
Pyth
>>>a[-1] 
m
>>>a[-1] = 'n'
>>>a
Python

You are doing the same here. You input your name and slice at a point. Note that string slicing starts at 0 for the first character and not 1.

你也在做同样的事情。你输入你的名字并在某一点上切分。注意,字符串切片从第一个字符的0开始,而不是1。

#2


1  

.1s in a format string is the syntax from the older printf-style formatting. There, it would have looked liked this:

格式字符串中的.1s是来自旧的printf格式的语法。在那里,它看起来就像这样:

>>> '%s %.1s %s' % ('Guido', 'van', 'Rossum')
'Guido v Rossum'

With the format string syntax, which you are using, this looks a bit different. Format specifiers are to be specified after a colon, like this:

使用您正在使用的格式字符串语法,这看起来有点不同。格式说明符应该在冒号之后指定,如下所示:

>>> '{} {:.1s} {}'.format('Guido', 'van', 'Rossum')
'Guido v Rossum'

Or using named groups:

或使用命名组:

>>> '{first} {middle:.1s} {last}'.format(first='Guido', middle='van', last='Rossum')
'Guido v Rossum'

This is using the precision to limit the string length, as documented (emphasis mine):

这是使用精度来限制字符串长度,如文档(强调我的):

The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with 'f' and 'F', or before and after the decimal point for a floating point value formatted with 'g' or 'G'. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The precision is not allowed for integer values.

精度是一个十进制数,指示在以'f'和'f'格式的浮点值的小数点后显示多少位,或者在以'g'或'g'格式的浮点值的小数点前后显示多少位。对于非数字类型,字段指示最大字段大小——换句话说,将从字段内容中使用多少字符。整数值不允许使用精度。

(Yes, I am aware that “van” is not a middle name…)

(是的,我知道“van”不是中间名……)