首页 > 编程语言 > 详细

Python编程从入门到实践-第2章-字符串

时间:2018-10-28 15:45:09      阅读:169      评论:0      收藏:0      [点我收藏+]

一、字符串:一系列字符,python中用一号括起来的即为字符串,可单可双

"This is a string."
‘This is also a string.‘

 

#引号灵活性的作用:可在字符串中包含引号和撇号

‘I told my friend,"python is my favorite language!"‘
"the language ‘Python‘ is named after Monty Python,not the snake."

 

#1.使用方法修改字符串大的小写

name = "ada lovelace"
print(name.title())

name = "ada lovelace"
print(name.upper())

name = "ada lovelace"
print(name.lower())

 

#2.合并(拼接)字符串

first_name = ‘ada‘
last_name = ‘lovelace‘
full_name = first_name + ‘ ‘ +last_name

print(full_name)

print ("Hello," + full_name.title() + "!")

message = "Hello," + full_name.title() + "!"
print (message)

 


#3.制表符或换行符添加空白
#制表符:\t

print (‘python‘)
print (‘\tpython‘)

 

#换行符:\n

print (‘python‘)
print (‘\npython‘)
print (‘Languages:\nPython\nC\nJavaScript‘)

 

 

 

#同时使用制表符和换行符
print (‘Languages:\n\tPython\n\tC\n\tJavaScript‘)


#4.删除空白:rstrip,lstrip,strip
#rstrip:删除右侧空白

#lstrip:删除左侧空白
#strip:删除两侧空白

favorite_language = ‘python ‘
print (favorite_language)

favorite_language = ‘python ‘
print (favorite_language.rstrip())


favorite_language = ‘python ‘
favorite_language = favorite_language.rstrip()
print (favorite_language)

 

favorite_language = ‘ python ‘
print (favorite_language.rstrip())
print (favorite_language.lstrip())
print (favorite_language.strip())

 

Python编程从入门到实践-第2章-字符串

原文:https://www.cnblogs.com/Hermione74/p/9865592.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!