[英]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,FIRSTNAME'. 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 ,名'。 As you can see I have made use of \' to do this but I was wondering if theres a more efficient way to do this.
如您所见,我已经使用了 \' 来执行此操作,但我想知道是否有更有效的方法来执行此操作。 thanks
谢谢
You don't need the slash before the single quotes.单引号前不需要斜杠。 Simply use the single quotes inside the double quotes like so:
print("'" + lname.upper() + "," + fname.upper() + "'")
只需在双引号内使用单引号,如下所示:
print("'" + lname.upper() + "," + fname.upper() + "'")
One alternative is to use template string literals as follows:一种替代方法是使用模板字符串文字,如下所示:
print(f"'{lname.upper()},{fname.upper()}'")
Template literals allow you to intermix values into your string.模板文字允许您将值混合到字符串中。 More examples uses of them to give an idea:
更多示例使用它们来给出一个想法:
x = 2
y = 3
print(f'{x} + {y} = {x + y}') # prints 2 + 3 = 5
Both the way you are doing it, and the one described above is perfectly valid.无论是你这样做的方式,还是上面描述的方式都是完全有效的。
Try this尝试这个
name = "'{},{}'".format(lname.upper(),fname.upper())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.