Python数据分析(中英对照)·Strings 字符串
1.2.5: Strings 字符串
字符串是不可变的字符序列。
Strings are immutable sequences of characters.
在Python中,可以将字符串括在单引号、引号或三引号中。
In Python, you can enclose strings in either single quotes,in quotation marks, or in triple quotes.
让我们看一下字符串上的几个常见序列操作。
Let’s look at a couple of common sequence operations on strings.
让我先定义一个字符串。
Let me first define a string.
让我们来看看“Python”
Let’s just go with "Python."
同样,如果我想知道我的字符串有多长,我可以使用len函数。
Again, if I wanted to find out how long is my string,I can use the len function.
或者,如果我想访问该字符串的第一个或最后一个元素,我可以使用我的通用序列操作。
Or if I wanted to access say the first or the last element of that string,I can use my common generic sequence operations.
我也会做切片。
I can also do slicing.
所以我可能想从字符串的最开始开始,取前三个对象。
So I might want to start from the very beginning of the string and take the first three objects.
在这种情况下,我指定切片的起点和终点。
In that case, I specify the starting point of the slice and the end point of the slice.
所以在这种情况下,我得到字母“Pyt”
So in this case, I get the letters "Pyt."
因此Python向我返回一个新字符串。
So Python returns a new string to me.
我也可以使用负索引进行切片。
I can also do slicing using negative indices.
例如,如果我键入S,减去3,Python将给出该序列中的最后三个字符,即h、o和n。
So for example, if I type S, minus 3, Python will give me the last three characters in that sequence,in that string, which are h, o, and n.
我还可以使用字符串测试成员资格。
I can also test for memberships using the strings.
例如,假设我想问,字符y是我字符串的一部分吗?
For example, imagine I wanted to ask, is the character y part of my string?
所以我可以输入y,我可以问,y在S中吗?
So I can type in my y, and I can ask, is y in S?
答案将会是真的。
And the answer is going to be True.
如果我使用大写字母Y,答案将是错误的。
If I use capital Y, the answer is going to be False.
字符串是讨论多态性的好地方。
Strings are a good place to talk about polymorphism.
请记住,多态性意味着运算符的操作取决于它应用于的对象的类型。
Remember, polymorphism means that what an operator does depends on the type of objects it is being applied to.
这就像在数学中,你必须为数字单独定义加法,而不是矩阵或其他对象。
This is just like in mathematics, where you have to define addition separately for numbers as opposed to matrices or some other object.
在Python中,我们可以使用加号将两个数字相加。
In Python, we can add two numbers together by using the plus sign.
例如,我可以问Python,12加12是多少?
So for example, I can just ask Python, what is 12 plus 12?
我也可以把两个字符串加在一起。
I can also add two strings together.
在这种情况下,该操作称为串联,而不是加法。
In that case, the operation is not called addition, but concatenation.
让我们在这里有一个字符串,“你好。”
So let’s have one string here, "hello."
我们可以将其添加到另一个字符串中。
We can add that to another string.
实际上Python所做的是:
And actually what Python does is the following:
它接受我的第一个字符串,接受第二个字符串,并将这两个字符串连接在一起。
It takes my first string, it takes the second string,and it concatenates those two strings together.
结果是一个新字符串,其中前两个字符串已放在一起。
The result is a new string where the previous two strings have been put together.
让我们先考虑数字的乘法运算。
Let’s think about the multiplication operation first with numbers.
比如,在数学中,如果我们说3乘以5,我们真正的意思是5加5加5,也就是15。
Say, in mathematics, if we say something like 3 times 5, what we really mean is 5 plus 5 plus 5, which would be 15.
如果我们在Python中有一个字符串——我们称之为s——如果我们键入三次s,Python会将其转换为s+s+s。
If we have a string in Python– let’s called that S–and if we type something like three times S,Python will turn this into S plus S plus S.
两个数字之间的加号表示加法,而两个字符串之间的加号表示串联。
A plus sign between two numbers means addition,whereas a plus sign between two strings means concatenation.
因此,在Python中说三次S,其中S是一个字符串对象,是非常有意义的。
Therefore to say three times S, where S is a string object,makes perfect sense in Python.
让我们试试这个。
Let’s try this out.
让我们定义一个字符串对象s。让我们使用“Python”。
Let’s define a string object S. Let’s just go with "Python".
如果我键入3次S,Python将获取该字符串并与自身连接。
If I type 3 times S, Python is going to take that string and concatenate with itself.
结果就是“PythonPythonPython”,一个新字符串。
So the result is "PythonPythonPython", a new string.
在我们这里看到的示例中,我们在两个对象之间应用了加号,这两个对象要么是数字和数字,要么是字符串和字符串。
In the examples that we looked at here, we were applying a plus sign between two objects that were either a number and a number or a string and a string.
为了使多态性工作,这两个对象必须是同一类型的。
In order for polymorphism to work, these two objects have to be of the same type.
因此,虽然将数字添加到数字中、将字符串添加到字符串中是有意义的,但将字符串添加到数字中或将字符串添加到数字中则没有意义。
So while it makes sense to add a number to a number and a string to string,it does not make sense to add a string to a number or vice versa.
让我们通过一个例子来了解这一点。
Let’s look at this through an example.
让我们尝试连接一个字符串和一个数字。
Let’s try to concatenate a string and a number.
所以首先,我需要我的绳子。
So first, I need my string.
我将得到类似于“8等于”的东西——我将尝试添加一个数字,比如说8。
I’m going to have something like "eight equals"– and I’ll try to add a number,let’s say 8.
这不起作用的原因是第一个对象是字符串,第二个对象是数字。
The reason this does not work is because the first object is a string and the second object is a number.
对于我来说,要运行这一行,我首先需要做的是将数字8转换成字符串。
For me to be able to run this line, what I need to do first is to take the number 8 and turn that into a string.
我可以使用str函数显式地将数字转换为字符串。
I can explicitly turn a number to a string by using the str function.
在这个例子中,我有两个字符串,所以说一个字符串加上一个字符串是有意义的。
In this case, I have two strings, so saying a string plus a string makes sense.
结果出来了。
And the result works out.
到目前为止,我们看到的字符串操作实际上都是泛型序列操作。
The operations we have seen for string so far have really all been generic sequence operations.
除了这些操作之外,字符串还有自己的方法,使您能够操作字符串。
In addition to these operations, strings also have their own methods that enable you to manipulate strings.
为了获得一个包含所有属性的目录,我键入dir、str表示字符串,Python为我提供了一个字符串可用的不同属性的长列表。
To get a directory of all attributes, I type dir, str for strings,and Python gives me a long list of different attributes that are available for strings.
让我们看看这里有什么样的帮助。
Let’s see what type of help is available to us right here.
例如,字符串函数“replace”对我来说很有趣,所以我可以只键入strreplace,然后在末尾输入一个问号。
For example, the string function "replace" looks interesting to me,so I can just type str replace, and I can enter a question mark at the end.
这给了我一个字符串替换方法的非常简短的描述。
This gives me a very brief description of the string replace method.
让我们通过一个例子来使用replace方法。
So let’s use the replace method through an example.
我将定义一个字符串,我们称它为“name”。
I’m going to define a string,let’s call it "name".
我将在这里使用“Tina Fey”作为字符串,我们想用小写字母T替换第一个大写字母T。
I’m going to use "Tina Fey" here as my string and we’d like to replace the first capital T with a lower case t.
我调用replace方法,我想用小写的T替换大写的T。
I call the replace method,and I’d like to replace the capital T with a lower case t.
因为字符串是不可变的对象,Python实际上不会修改字符串。
Because strings are immutable objects, Python doesn’t actually modify your string.
相反,它会返回一个新字符串给您。
Instead what it does — it returns a new string to you.
如果我想保留这个新字符串,我必须将它赋给某个变量。
If I’d like to keep this new string, I have to assign it to some variable.
例如,我可以称之为“新名字”,它由原来的名字和字母T组成。
So for example, I could call this "new_name",which consists of the original name with the letter T replaced.
如果我现在看一下我原来名字的内容,你会发现T仍然是大写的。
If I now look at the content of my original name,you’ll see that the T remains capitalized.
但如果我问,我的新名字是什么?
But if I ask, what is my new name?
大写字母T现在显示为小写字母T。
The capital T now appears as a lowercase t.
让我们继续我们的例子。
Let’s continue with our example.
但这一次,让我们尝试拆分方法。
But this time, let’s try the split method.
split方法获取一个字符串并将其分解为子字符串。
The split method takes a string and breaks that down into substrings.
您必须指定要用于拆分字符串的字符。
What you have to specify is the character you would like to use for splitting the string.
我还可以使用其他字符串方法将这些字符串转换为小写或大写字母。
I can also take these strings and turn them into lowercase or uppercase letters using other string methods.
让我们找出这个对象的类型——它是一个列表。
Let’s find out the type of this object –it’s a list.
因此,我们可以询问该列表中包含多少对象。
Therefore we can ask how many objects are contained within that list.
Python告诉我们这里有两个对象。
And Python tells us we have two objects there.
因为它是一个列表,所以我们可以通过各个对象的位置来访问它们,所以写名称方括号0或名称方括号1是有意义的。
Because it’s a list, we can access individual objects by their position so it makes sense to write names square brackets 0, or names square bracket 1.
我们还可以执行以下操作:
We can also do the following:
我们可以提取列表中的第一个对象,我们可以询问该对象的类型。
We can extract the first object in that list,and we can ask what is the type of that object.
现在我们知道它是一个字符串,我们可以调用一些字符串方法来修改该名称。
Now that we know it’s a string, we can call some string methods to modify that name.
让我们用这个字符串命名方括号0,然后调用上面的方法。
So let’s take that string — names square brackets 0, and let’s call the upper method.
这会将名称“Tina”全部大写。
This turns the name "Tina" into all uppercase.
我们可以类似地获取位置1处的字符串,并使用lower方法将其转换为小写字符串。
We could similarly take, let’s say, the string at location 1,and turn that into a lowercase case string using the lower method.
金点网络-全网资源,一网打尽 » Python数据分析(中英对照)·Strings 字符串
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
- 是否提供免费更新服务?
- 持续更新,永久免费
- 是否经过安全检测?
- 安全无毒,放心食用