合并(拼接)字符串 "+"

 

1、"+"的用法:


1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 first_name = "ada" 4 last_name = "lovelace" 5 full_name = first_name+" "+last_name 6 print(full_name) 7 8 执行结果为: 9 C:\Users\tby\AppData\Local\Programs\Python\Python37\python.exe C:/py3/rumen_shijian/hebing_+.py
10 ada lovelace 11 12 进程已结束,退出代码0

 

 

2、"+"和.title()的用法:

 1 first_name = "ada"
 2 last_name = "lovelace"
 3 full_name = first_name+" "+last_name
 4 print("Hello,"+ full_name.title() + "!")   #full_name中的字符 首字母大写
 5 
 6 
 7 执行结果为:
 8 
 9 C:\Users\tby\AppData\Local\Programs\Python\Python37\python.exe C:/py3/rumen_shijian/hebing_+.py
10 Hello,Ada Lovelace! 11 12 进程已结束,退出代码0

 

3、使用拼接来创建消息,再把整条消息都储存在一个变量中:

 1 first_name = "ada"
 2 last_name = "lovelace"
 3 full_name = first_name+" "+last_name
 4 message = "Hello,"+ full_name.title() + "!"
 5 print(message)
 6 
 7 执行结果为:
 8 
 9 C:\Users\tby\AppData\Local\Programs\Python\Python37\python.exe C:/py3/rumen_shijian/hebing_+.py
10 Hello,Ada Lovelace! 11 12 进程已结束,退出代码0

 

posted on 2017-11-22 15:11  wl404  阅读(217)  评论(0编辑  收藏  举报

导航