令人费解的是,我可以在第12行使用+=
操作符代替-=
而没有问题。你知道吗
课程:
message = input("Enter a Message: ")
new_message = ""
VOWELS = "aeiou"
print()
for letter in message:
if letter.lower() not in VOWELS:
new_message -= letter
print("A new string has been created:", new_message)
print("Your message without vowels is:", new_message)
input("\n\nPress the enter key to exit.")
错误消息:
Traceback (most recent call last):
File "C:\Python31\no vowels (from book).py", line 12, in <module>
new_message -= letter
TypeError: unsupported operand type(s) for -=: 'str' and 'str'
编辑:如果我听起来很无知,忘了提,我是编程新手
字符串支持
+
和+=
运算符,因为对于一对字符串,加法可以解释为串联。它们不支持-
或-=
,因为没有有意义的字符串操作可以将减法转换成。你知道吗如果您使用的是数字类型,通常会支持这两种运算符。你知道吗
字符串还支持其他一些运算符:可以将字符串乘以整数以重复多次。您可以使用
%
运算符来执行较旧的printf
样式的字符串格式设置(在右侧使用一个非元组参数,或者使用一个由多个参数组成的元组)。你知道吗相关问题 更多 >
编程相关推荐