在Python中,连接符主要用于连接字符串或其他可迭代对象。以下是一些常见的连接符用法:
+ 连接符:用于连接两个字符串。str1 = "Hello"str2 = "World"result = str1 + " " + str2print(result) # 输出: Hello World* 连接符:用于将一个字符串重复多次。str1 = "Hello"result = str1 * 3print(result) # 输出: HelloHelloHellojoin() 方法:用于连接字符串列表。str_list = ["Hello", "World"]result = " ".join(str_list)print(result) # 输出: Hello Worldformat() 方法:用于在字符串中插入变量或表达式。name = "Alice"age = 25result = "My name is {} and I am {} years old.".format(name, age)print(result) # 输出: My name is Alice and I am 25 years old.注意:连接符只能用于连接字符串或可迭代对象,对于其他类型的数据需要先转换为字符串才能连接。

