繁体   English   中英

如何让我的代码用单引号打印?

[英]How do I get my code to print with single quotes?

email = email.split('@', 1)[0]
fname = email.split('.', 1)[0]
lname = email.split('.', 1)[1]
name =("\'"+lname.upper()+","+fname.upper()+"\'")
print(name)

This is my code, email is given as a string with the format firstname.lastname@example.com Now everything works fine except when it outputs it will output in the form LASTNAME,FIRSTNAME but I need it to print with single quotes like 'LASTNAME ,名'。 如您所见,我已经使用了 \' 来执行此操作,但我想知道是否有更有效的方法来执行此操作。 谢谢

单引号前不需要斜杠。 只需在双引号内使用单引号,如下所示: print("'" + lname.upper() + "," + fname.upper() + "'")

一种替代方法是使用模板字符串文字,如下所示:

print(f"'{lname.upper()},{fname.upper()}'")

模板文字允许您将值混合到字符串中。 更多示例使用它们来给出一个想法:

x = 2
y = 3
print(f'{x} + {y} = {x + y}') # prints 2 + 3 = 5

无论是你这样做的方式,还是上面描述的方式都是完全有效的。

尝试这个

name = "'{},{}'".format(lname.upper(),fname.upper())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM