[TOC]
字符串就是一系列的字符,在Python中,用引号括起来的都是字符串,其中引号可以是单引号,也可以是双引号.可以嵌套.
使用方法修改字符串的大小写
对于字符串,可执行的最简单的操作之一是修改其中单词的大小写
name = "ada lovelace"
print(name.title())//每个单词首字母大写
print(name.upper())//所有字母大写
print(name.lower())//所有字母小写
输出为:
Ada Lovelace
ADA LOVELACE
ada lovelace
在print()语句中,方法title()出现在这个变量的后面.方法是python可对数据执行的操作.在name.title()中,name后面的句点(.)让python对变量name执行方法title()的指定操作.每个方法后面都跟着一对括号,这是因为方法通常需要额外的信息来完成其工作.这种信息是在括号内提供的.
拼接字符串
python中用+来拼接字符串
使用制表符或者换行符来添加空白
使用制表符
>>>print("python")
python
>>>print("\tpython")
python
使用换行符和制表符
>>>print("Language:\t\nPython\t\nC\t\nJavaScript")
Languages:
Python
C
JavaScript
删除空白:要永久删除这个字符串中的空白,必须将删除操作的结果存回到变量中
>>> favorite_langage
' python '
>>> favorite_langage=favorite_langage.strip()//strip()和lstrip()函数删除空白
>>> favorite_langage
'python'
Python之禅
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
网友评论