Nic*_*las -1 python string subtraction
我试图从for循环内的字符串中删除一个空格.我能够让它为添加空间而工作,但删除不起作用.
这是我的代码:
letterHeight = 10
def nLetter():
x = 0
diagonal = ""
vertical = " "
while x < letterHeight:
print "*"+diagonal+"*"+vertical+"*"
diagonal += " "
vertical -= " "
x += 1
nLetter()
Run Code Online (Sandbox Code Playgroud)
错误: TypeError: unsupported operand type(s) for -=: 'str' and 'str'
字符串不支持减法; 应删除哪一个空格?改为使用切片:
vertical = vertical[:-1]
Run Code Online (Sandbox Code Playgroud)
这将通过从除最后一个字符之外的所有字符创建新字符串来删除最后一个字符:
>>> "abcd"[:-1]
'abc'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
876 次 |
| 最近记录: |