我试图在Python中连接字符串和数字.当我尝试这个时它给了我一个错误:
"abc" + 9
Run Code Online (Sandbox Code Playgroud)
错误是:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
"abc" + 9
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)
为什么我无法做到这一点?
如何可以我将字符串和Python中的号码是多少?
我有代码:
num_for_loops = 0
for line in lines:
if line.strip().startswith('for '):
num_for_loops += 1
return num_for_loops
Run Code Online (Sandbox Code Playgroud)
我需要将它浓缩为尽可能少的线条.有没有办法通过组合for和if语句来做到这一点?背景是无关紧要的.