python中字符串的合并(f字符串,在变量中使用字符串)

 

>>> a = "string1"
>>> b = "string2"
>>> print(ab)  ## 直接输出报错
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in <module>
    print(ab)
NameError: name 'ab' is not defined
>>> ab = f"{a} {b}"   ## 使用f合并字符串
>>> print(ab)
string1 string2
>>> print(ab,"strints")
string1 string2 strints
>>> print(f"{ab} strints")
string1 string2 strints

 

>>> print("xxxx")
xxxx
>>> print("yyyy")
yyyy
>>> print("xxxx""yyyy")
xxxxyyyy
>>> print("xxxx","yyyy")
xxxx yyyy
>>> a = "mmmm"
>>> b = "nnnn"
>>> c = "oooo"
>>> print(bc)
Traceback (most recent call last):
  File "<pyshell#111>", line 1, in <module>
    print(bc)
NameError: name 'bc' is not defined
>>> print(b,c)
nnnn oooo
>>> print(a,b,c)
mmmm nnnn oooo

 

posted @ 2020-12-15 18:12  小鲨鱼2018  阅读(792)  评论(0编辑  收藏  举报