令人费解的是,我可以在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.")
Run Code Online (Sandbox Code Playgroud)
错误信息:
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'
Run Code Online (Sandbox Code Playgroud)
编辑:如果我听起来无知,忘了提,我是编程的新手
字符串支持+和+=运算符,因为对于一对字符串,可以将加法解释为连接.它们不支持-或者-=,因为没有有意义的字符串操作来将减法转换为.
如果您使用的是数字类型,则通常会支持这两种运算符.
字符串还支持其他一些运算符:您可以将字符串乘以整数以重复多次.您可以使用%运算符来执行较旧printf的字符串格式化(右侧有一个非元组参数,或者多个参数的元组).
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |