The first data type we'll introduce is string .
Although the string seems simple , But they can be used in many different ways .
A string is a series of characters , stay Python in , It's all strings in quotation marks , The quotation marks can be single quotation marks , Or double quotes , As shown below :
"This is bcbx."
'This is bcbx_home.'
Copy code
This flexibility allows you to include quotation marks and apostrophes in strings :
'I told my friend, "I love bcbx!"'
"The language 'Python' is named after Monty Python, not the snake."
Copy code
Here are some ways to use strings
Use the method to modify the size of the string
For strings , One of the simplest things you can do is change the case of the words in it . Look at the code below , And try to judge its function :
name = "come on bcbx"
print(name.title())
Copy code
Execution results :
In this example , A lowercase string "come on bcbx" Stored in variable name in . stay print() In the sentence , Method title() It appears after this variable .
The method is Python Operations that can be performed on data .
stay name.title() in ,name Period after (. ) Give Way Python The variable name Execution method title() Specified operation . Each method is followed by a pair of parentheses , This is because methods usually require additional information to do their job . This information is provided in brackets . function title() No additional information is needed , So the brackets after it are empty .
title() Show each word in uppercase , Change the initial of each word to uppercase . It's very useful , Because you often need to think of names as information . for example , You may want the program to be worth Ada 、ADA and ada Treat as the same name , And show them all as Ada .
There are several other useful ways to handle case . for example , To change the string to all uppercase or lowercase , You can do it like this :
name = "Come on Bcbx"
print(name.upper())
print(name.lower())
Copy code
The output of this code is as follows :
When storing data , Method lower() It is useful to . A lot of times , You can't rely on users to provide the correct case , So you need to convert the string to lowercase first , Store them again . When you need to display this information later , Then convert it to the most appropriate case .