简体   繁体   English

使用“”“”“”格式的Python打印格式

[英]Python printing format using “”“ ”“” format

I am writing an email application to send mass emails. 我正在编写一个电子邮件应用程序以发送大量电子邮件。 In the body of the email I want to insert the first name from a list of names. 在电子邮件的正文中,我想从名称列表中插入名字。 But I can't seem to figure out why this code isn't working. 但是我似乎无法弄清楚为什么此代码无法正常工作。 Any suggestions are most appreciated. 任何建议是最赞赏的。

first_name = " "

body = """\
Dear {},
Here is new email.
Thanks.
Mike """.format(first_name)

def send_test_email(body):
  first_name = 'mike'
  print(body)

send_test_email(body)

Output: 输出:

Dear , 亲 ,

Here is new email. 这是新邮件。

Thanks. 谢谢。

Mike 麦克风

You have already assigned what body is. 您已经分配了body是什么。 So even if you change value of parameter ( first_name ) in format later, it does not affect the original body . 因此,即使您以后以format更改参数( first_name )的值,也不会影响原始body

first_name = " "

body = """\
Dear {},
Here is new email.
Thanks.
Mike """

def send_test_email(body):
  first_name = 'mike'
  print(body.format(first_name))

send_test_email(body)

# Dear mike,
# Here is new email.                                              
# Thanks.   
# Mike                                                       

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

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