1.来自C语言的%方式
print('%s %s' % ('Hello', 'world'))
>>> Hello world
2.常用的+号方式
str_1 = 'Hello world! '
str_2 = 'My name is Python猫.'
print(str_1 + str_2)
>>>Hello world! My name is Python猫.
print(str_1)
>>>Hello world!
3.join()拼接方式`
str_list = ['Hello', 'world']
str_join1 = ' '.join(str_list)
str_join2 = '-'.join(str_list)
print(str_join1) >>>Hello world
print(str_join2) >>>Hello-world
4.f-string方式
name = 'world'
myname = 'python_cat'
words = f'Hello {name}. My name is {myname}.'
print(words)
>>> Hello world. My name is python_cat.
6.字符串转换成变量名方法一
>>> list1 = ['A', 'B', 'C', 'D']
>>> for i in list1:
>>> exec(f"{i} = []")
>>> A
[]
>>> exec('x = 1 + 2')
>>> x
3
方法二
>>> list1 = ['A', 'B', 'C', 'D']
>>> for i in list1:
>>> globals()[i] = []
>>> A
[]
来源:https://blog.csdn.net/TONYforlife/article/details/102722971