一、字符串:一系列字符,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())
原文地址:https://www.cnblogs.com/Hermione74/p/9865592.html
时间: 2024-10-12 15:42:23