python两列字符串如何合并?
python两列字符串合并的方法:
1、在很多情况下,我们都需要合并字符串。例如:需要将姓氏与名字存储在不同的变量中,然后显示的时候再将他们合二为一
first_name = 'oliver' last_name = 'smith' full_name = first_name + ' ' + last_name print(full_name)
打印结果:
立即学习“Python免费学习笔记(深入)”;
oliver smith
2、python中使用(+)号来合并字符串
这种字符串合并的方法称为拼接,可使用存储在变量中的信息来创建完整的信息
first_name = 'oliver' last_name = 'smith' full_name = first_name + ' ' + last_name print('Hello ' + full_name.title() + '!')
打印结果:
立即学习“Python免费学习笔记(深入)”;
Hello Oliver Smith!
此处的title()方法将单词的首字母大写了,让姓名显示更格式化
3、也可以创建用一个变量来存储整条语句
first_name = 'oliver' last_name = 'smith' full_name = first_name + ' ' + last_name msg = 'Hello ' + full_name +'!'print(msg)
这样,print语句中显示的就没那么复杂了!
推荐教程:《python视频教程》
以上就是python两列字符串如何合并?的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号