So far, we’ve only used numbers. Now, let’s learn another data type: strings .
Getting to Know Python Strings
Strings in Python are sequences of characters (as in most programming languages), which can contain letters, numbers, punctuation, and spaces. We put the string in quotes so that Python knows where the string starts and ends.
For example, the following is a common string:
>>> "Hello World!" 'Hello World!'
To enter a string, simply enter a double quote
"followed by the desired string literal, then end the string with another double quote. You can also use single quotes
'instead of double quotes. But for simplicity, in this tutorial, we just use double quotes.
Tip: Everyone should pay attention that the single and double quotation marks here are both single and double quotation marks in English (half-width characters).
storage of strings
Strings can also be stored into variables, just like we did with numbers.
>>> myString="This is my string" >>> myString 'This is my string'
Hint: Python, as a dynamic language, can freely change the type of its variables. This feature is called dynamic typing, and it makes writing code more efficient, but it also makes it harder to read and maintain the code.If a variable has previously stored a string, this does not affect our subsequent assignment of a number to it; similarly, if a variable has previously stored a number, it will not affect the assignment of a string to it.
For example, we first assign the variable myString to the number 5. At this time, we can see that the content of myString is a number. Next, we reassign myString, this time assign a string “This is a string” to the variable, you can see that the content of myString has become a string.
>>> myString=5 >>> myString 5 >>> myString="This is a string" >>> myString 'This is a string'
Judgment between strings and numbers
What if you put a number in quotes? Is it a string or a number?
In Python, if you put a number in quotes, it will be treated as a string. As we mentioned earlier, a string is a sequence of characters (even if occasionally some of them are numbers). E.g:
>>> numberEight=8 >>> stringEight="8"
numberEight is a number and stringEight is a string.
To see the difference between them, we add them up:
>>> numberEight+numberEight 16 >>> stringEight+stringEight '88'
We add 8 to the number 8 and we get the number 16. However, when we use the + operator for “8” and “8”, we just concatenate the strings directly together, resulting in “88”, which concatenates the two strings into one longer string.
Python string concatenation
As you can see, we can use the + operator with strings, but the results are quite different from using the + operator with numbers. When you concatenate two strings with +, the second string is appended to the end of the first string, resulting in a new string as follows:
>>> greeting = "Hello " >>> name = "Johnson" >>> greeting+name
‘Hello Johnson’ Here two variables, greeting and name, are created and assigned a string value of “Hello” and “Johnson”, respectively. When we add these two variables together, the two strings are combined into a new string “Hello Johnson”.
It should be noted here that Python does not automatically add a space to strings, so in order to separate two words, we need to add a space to the end of the first string.
Methods of Python Strings
Methods are functions that Python can perform on data.
For example, in the example below, in myName.title(), the period after myName
.tells Python to perform the action specified by the method title on the variable myName.
>>> myName="johnson" >>> myName.title() 'Johnson'
Next, let’s take a look at a few commonly used string methods.
The title() method displays each word in uppercase, that is, capitalizes the first letter of each word. In the above example, we used the title() method to change “johnson” to “Johnson”.
The upper() method rewrites the string to all uppercase letters, in the example below, the upper() method changes “johnson” to “JOHNSON”.
>>> myName.upper() 'JOHNSON'
The lower() method rewrites the string to all lowercase letters, in the example below, the lower() method changes “JOHNSON” to “johnson”.
>>> "JOHNSON".lower() 'johnson'