簡體   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