Python基础 - 字符串插入字母与数字

python字符串插入字母与数字:

错误的方法:

str = "abcde"
str = str + str(5)

TypeError: 'str' object is not callable 
# coding=utf8
str = '你的分数是:'
num = 82
num = bytes(num)
text = str+num+'分 | 琼台博客'
print text

TypeError: can only concatenate str (not "bytes") to str

 正确姿势: 

s = "abc123"
想在s中1后面插入love
s1 = "abc1%s23" %"love"
print(s1)
想在s中a后面插入2
s2 ="a%dbc123" %(2)
print(s2)
想同时进行上面2个操作
s3 = "a%dbc1%s23" %(2,"love")

很实用! 

break跳出当前循环,从 for 或 while 循环中终止,任何对应的循环 else 块将不执行。

while True:
    s = (input("Enter something:"))
    if s == 'quit':
        break
    print('输出字符串的长度',len(s))
print('Done')  

输出结果如下:
>>> 
Enter something:quit
Done

continue终止本轮循环,开始下一轮循环

while True:
    s = (input("Enter something:"))
    if s == 'quit':
        break
    if len(s) < 3:
        print('Too small')
    continue
    print('输出字符串的长度',len(s))
print('Done')

>>> 
Enter something:12
Too small
Enter something:123
Enter something:1234
Enter something:quit
Done

if():continue 语句的作用是:如果满足if里面的条件,则不执行循环语句里面的剩余内容,跳出循环,执行下一次循环。

TypeError: 'str' object is not callable 

该错误TypeError: 'str' object is not callable字面上意思:就是str不可以被系统调用,

其实原因就是:你正在调用一个不能被调用的变量或对象,具体表现就是你调用函数、变量的方式错误.

所以,这个错误想表达的就是:str()是系统自带的,你不能在用它的时候自己同时定义一个别的叫做str的变量,这样会冲突.

你可能感兴趣的:(python)