【Python基础编程012 ● Python入门 ● 字符串与整数的运算 】


 ---------Python基础编程---------

Author : AI菌


【内容讲解】

# 字符串与整数不能相加
# "hello" + 10  # TypeError: must be str, not int

# 把字符串复制n次,就是连续拼接n次
print("hello" * 3)  # hellohellohello

# 两个字符串可以相加 , 这里的 + 是字符串连接符的意思,用来连接两个字符串的
print("hello" + "python")  # hellopython
print("hello" + "python" + "ai")  # hellopythonai

# 布尔类型的值可以与数字直接运算, True作为1,False作为0
print(True + 1)  # 2
print(False + 1)  # 1

【代码演示】

# 字符串与整数不能相加
# "hello" + 10  # TypeError: must be str, not int

# 把字符串复制n次,就是连续拼接n次
print("hello" * 3)  # hellohellohello

# 两个字符串可以相加 , 这里的 + 是字符串连接符的意思,用来连接两个字符串的
print("hello" + "python")  # hellopython
print("hello" + "python" + "ai")  # hellopythonai

# 布尔类型的值可以与数字直接运算, True作为1,False作为0
print(True + 1)  # 2
print(False + 1)  # 1

【往期精彩】

▷【Python基础编程196 ● 读取文件的4种方式】
▷【Python基础编程197 ● 读取文件的4种方式】
▷【Python基础编程198 ● 读取文件的4种方式】
▷【Python基础编程199 ● Python怎么读/写很大的文件】
▷【Python基础编程200 ● 读取文件的4种方式】
▷【Python基础编程201 ● 读取文件的4种方式】
▷【Python基础编程202 ● 读取文件的4种方式】
▷【Python基础编程203 ● 读取文件的4种方式】

【加群交流】



原文地址:https://www.cnblogs.com/hezhiyao/p/13518268.html