Strings 字符串

# 1.2.5: Strings 字符串

Strings are immutable sequences of characters.

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.

Let’s just go with “Python.”

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.

So in this case, I get the letters “Pyt.”

So Python returns a new string to me.

I can also do slicing using negative indices.

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.

For example, imagine I wanted to ask, is the character y part of my string?

So I can type in my y, and I can ask, is y in S?

And the answer is going to be True.

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.

In Python, we can add two numbers together by using the plus sign.

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.

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.

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.

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.

Therefore to say three times S, where S is a string object,makes perfect sense in Python.

Let’s try this out.

Let’s define a string object S. Let’s just go with “Python”.

If I type 3 times S, Python is going to take that string and concatenate with itself.

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.

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.

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.

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.

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.

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.

So let’s use the replace method through an example.

I’m going to define a string,let’s call it “name”.

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.

I call the replace method,and I’d like to replace the capital T with a lower case t.

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.

So for example, I could call this “new_name”,which consists of the original name with the letter T replaced.

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?

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.

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.

So let’s take that string – names square brackets 0, and let’s call the upper method.

This turns the name “Tina” into all uppercase.

We could similarly take, let’s say, the string at location 1,and turn that into a lowercase case string using the lower method.

转载请注明: 金点帝国 Strings 字符串

上一篇 Ranges 范围
1.2.4: Ranges 范围范围是不可变的整数序列，通常用于for循环。Ranges are immutable sequences of integers,and they are commonly used in for loops
2021-10-18 Dictionaries 字典
1.2.7: Dictionaries 字典字典是从键对象到值对象的映射。Dictionaries are mappings from key objects to value objects.字典由键：值对组成，其中键必须是不可变的，值可
2021-10-18
目录