python - 在 Python 中打印变量和字符串
问题描述
我正在尝试打印以下行:
“我的名字是约翰史密斯。”
“我的名字是约翰史密斯,我住在芝加哥”
我住在芝加哥”
我的代码如下:
name = 'John Smith'
age = 25
location = 'CHICAGO' # change this to lower case when printing
print('My name is %s.')
print('My name is %s, and I live in %s' )
print(f'My name is {name}.')
'and I live in location.lower()
如何从顶部获得结果?
解决方案
#!/usr/bin/env python3
name = 'john smith'
age = 25
location = 'chicago'
print ('My name is %s, and I live in %s. I am %s years old.' % (name, location, str(age)))
输出:
我的名字是约翰史密斯,我住在芝加哥。我25岁。
顺便说一句,我建议您阅读这篇文章:https ://realpython.com/python-string-formatting/
推荐阅读
- json - 有没有办法 JSON.stringify 一个对象并让它的类型是 JSON?
- go - 使用中间处理函数来路由请求
- python - 如何在颜色条上同时放置 y 轴标签和标题
- javascript - 为什么网页无法使用下载的sqlite3数据库?
- python - 颜色,matplotlib 中部分文本的粗体(使用 LaTeX),带有 pdf 输出?
- php - laravel socialite - 生产中未触发回调
- java - 通过 RxJava 缓存 API 响应
- internet-explorer - 视频渲染在 IE 上不起作用
- javascript - 尝试提交交易时出现 TypeError
- python - 需要从另一个 python 脚本调用需要命令行参数的 python 脚本