Python编程从入门到实践-第2章-字符串
Posted hermione74
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python编程从入门到实践-第2章-字符串相关的知识,希望对你有一定的参考价值。
一、字符串:一系列字符,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.制表符或换行符添加空白
#制表符:
print (‘python‘) print (‘ python‘)
#换行符:
print (‘python‘) print (‘ python‘) print (‘Languages: Python C JavaScript‘)
#同时使用制表符和换行符
print (‘Languages:
Python
C
JavaScript‘)
#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章-字符串的主要内容,如果未能解决你的问题,请参考以下文章
社区共读《Python编程从入门到实践》第7,8,9天阅读建议
社区共读《Python编程从入门到实践》第7,8,9天阅读建议