1.format 格式化輸出,在{}的地方替換為變量的值甲葬。
add=3+4
print('3+4的值是{}'.format(add)) ? #運(yùn)行結(jié)果:“3+4的值是7”
print(''3+4的值是',add) ???#運(yùn)行結(jié)果:"3+4的值是 7" ? ? #7前面空一個(gè)字符 ? ? ? ? ?
com="complex"
comp="complicated"
print("{} is better than {}".format(com,comp)) ?#運(yùn)行結(jié)果?complex is better than complicated,語(yǔ)句中花括后有空格
print(com,"is better than",comp) ? ? ? ? ? ? ? ? ? ? ???#運(yùn)行結(jié)果?complex is better than complicated贸辈。變量會(huì)自動(dòng)會(huì)加空格隔開(kāi)
總結(jié):用format的好處惋鸥,會(huì)和上下文很好地融在一起贺嫂,而不會(huì)像直接用變量一樣际起,會(huì)生硬地隔開(kāi)拾碌。
2.布爾邏輯值的運(yùn)算及注意事項(xiàng)
a = True ? ? ? ??#只能是True,TRUE和其他大小寫(xiě)形式都不行
print(a and "a=t" or "a=f") ? ? ? ??#and和or只能小寫(xiě)
3.字符串的計(jì)算
print('\n\n\n歡迎來(lái)到python實(shí)戰(zhàn)圈,') ? # \n表示換行
print('\t歡迎來(lái)到python實(shí)戰(zhàn)圈') ? ?# \t表示縮進(jìn)兩格
print('let\'s go') ? # \轉(zhuǎn)移字符串
print("let\'s go")
log_1_str="good good eat"
log_2_str="day day up"
long_str=log_1_str+log_2_str
print("拼接后的字符串是:",long_str)
welcome="Welcome to our home"
print("首字母大寫(xiě)",welcome.title())
print("段首大寫(xiě)",welcome.capitalize())
print("全部大寫(xiě)",welcome.upper())
print("全部小寫(xiě)",welcome.lower())
print("大小寫(xiě)轉(zhuǎn)換",welcome.swapcase())
str2="1 2 3 4 5 6 7"
print(str2.split()) ? ???#運(yùn)行結(jié)果:['1', '2', '3', '4', '5', '6', '7']
print('以換行符為分割','1+2\n+3+4\n+5+6'.splitlines()) ???#運(yùn)行結(jié)果:以換行符為分割 ['1+2', '+3+4', '+5+6']
str3=" 1 2 3 4 5 6 7 "
print('去除字符串兩端的空白',str3.strip())
print('去除字符串左端的空白',str3.lstrip())
print('去除字符串右端的空白',str3.rstrip())
4.數(shù)據(jù)類(lèi)型及轉(zhuǎn)換
print('number1的數(shù)據(jù)類(lèi)型是:')
print(type(number1))
print(int(number1))
print(str(number1))
print(int(number1))