字符串插值使用python

 司文龙2502933797 发布于 2023-02-09 23:33

我是新手,学习如何在字符串中使用字符串插值,并且我很难得到这个例子,我正在努力实际打印正确的结果.

我试过这样做:

print "My name is {name} and my email is {email}".format(dict(name="Jeff", email="me@mail.com"))

它错误地说 KeyError: 'name'

然后我尝试使用:

print "My name is {0} and my email is {0}".format(dict(name="Jeff", email="me@mail.com"))

它打印出来

My name is {'email': 'me@mail.com', 'name': 'Jeff'} and my email is {'email': 'me@mail.com', 'name': 'Jeff'}

所以我试着这样做:

print "My name is {0} and my email is {1}".format(dict(name="Jeff", email="me@mail.com"))

它错误地说 IndexError: tuple index out of range

它应该给我以下输出结果:

My name is Jeff and my email is me@mail.com

谢谢.

1 个回答
  • 只需删除以下呼叫dict:

    >>> print "My name is {name} and my email is {email}".format(name="Jeff", email="me@mail.com")
    My name is Jeff and my email is me@mail.com
    >>>
    

    以下是字符串格式的语法参考.

    2023-02-10 00:04 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有