Chapter V methods and skills
method
Case of string
1.title() method: capitalize the first letter of the string. Operation mode – > string Title () or variable name title()
name = 'i love you' print(name.title()) result: I Love You
2.upper() method: capitalize all letters of the string. Operation mode – > string Upper() or variable name upper()
name = 'i love you' print(name.upper()) result: I LOVE YOU
3.lower() method: lowercase all letters of the string. Operation mode – > string Lower() or variable name lower()
name = 'I LOVE YOU' print(name.lower()) result: i love you
Conclusion: these methods can effectively save data. For example, some license plates are all capitalized. At this time, you need to use the upper method to solve this problem.
List sorting method
1.sort() method: permanently sort the list in the order of 26 letters. Operation mode – > list name sort()
Note: tuples cannot be arranged because the data in tuples cannot be modified!!!!
name = ['bmw','toyota','audi','subaru'] name.sort() print(name) result: ['audi', 'bmw', 'subaru', 'toyota']
The above is sorted in the positive direction of letters. Of course, we can also sort them in the reverse direction!! Operation mode – > list name sort(reverse=True)
name = ['bmw','toyota','audi','subaru'] name.sort(reverse=True) print(name) result: ['toyota', 'subaru', 'bmw', 'audi']
2.sorted() method: the difference between this method and sort method is that it only sorts the list temporarily, rather than permanently modifying the data order in the list. Operation mode – > sorted (list name)
name = ['bmw','toyota','audi','subaru'] print(sorted(name)) #Print the sorted list print(name) #Print out the original list result: ['audi', 'bmw', 'subaru', 'toyota'] ['bmw', 'toyota', 'audi', 'subaru']
3.reverse() method: print the elements in the list upside down. Operation mode – > list name reverse()
name = ['bmw','toyota','audi','subaru'] name.reverse() print(name) result: ['subaru', 'audi', 'toyota', 'bmw']
Query method
The query method is applicable to lists, tuples and strings
1.len() method: query the length of data. Operation mode – > len (data)
name = ['bmw','toyota','audi','subaru'] print(len(name)) result: 4
**2.index() * * method: obtain the subscript of the specified data according to the content. Operation mode – > list name Index or list name Index (data, starting point)
name = ['bmw','toyota','audi','subaru'] print(name.index('bmw')) result: 0
3.count() method: counts the number of occurrences of data. Operation mode – > name list Count (data) or variable name Count (data)
number = [1,2,3,1,2,5,6,2,1,4,7,2,3,1,1,1] print(number.count(1)) result: 6
4.find() method: obtain the corresponding subscript through the content. Operation mode – > string name Find (content) or string name Find (content, starting point)
name = 'Xiaohong is great' print(name.find('red')) result: 1
Segmentation method
1.strip() method: remove the spaces on the left and right sides. Operation mode – > string name strip()
name = ' Xiao Hong' print(name) print(name.strip()) result: Xiao Hong Xiao Hong
2.split() method: split. The string is split according to the specified content, and the result is stored in a list. Operation mode – > string name split('split element ')
name = 'Xiaohong is great' print(name.split('red')) result: ['Small', 'that 's great']
Judgment method
1.isdigit() method: judge whether the string is a pure number -- > and the result is bool value (True, False). Operation mode – > string name isdigit()
name = 'Xiaohong is great' number = '1123456' print(name.isdigit(),number.isdigit()) result: False True
2.isalpha() method: judge whether the string is a pure character – > the result is bool value (True, False). Operation mode – > string name isalpha()
name = 'Xiaohong is great' number = '1123456' print(name.isalpha(),number.isalpha()) result: True False
3. Endswitch() method: judge whether the trailing character is equal to the specified value – > the result is bool value (True, False). Operation mode – > string Endswitch ('end value ')
name = 'Xiaohong is great' print(name.endswith('stick')) result: True
Job: 1.Learn and use these methods to complete the operation you want
skill
Transfer character
I go to a restaurant and always order (Cantonese style roast meat, pot wrapped meat, balsam pear soup, boiled broccoli) - > for a long time, I see the boss and say 'the same as before' Does the boss know what I want?
The old -- > has some special meanings
In programming There are some symbols, characters They are data with special meaning, with some special functions -- > escape characters
\N -- > line feed
name = 'Xiaohong is great' print(f'\n{name}') result: Xiaohong is great
\T -- > tab (TAB), usually 4 spaces
name = 'Xiaohong is great' print(f'\t{name}') result: Xiaohong is great
Two \ -- > normally output a slash
A slash + '– > normally outputs a quotation mark
\A -- > system prompt tone
r'asdasd\afdaufh\nasdiouad\t'
r 'string content' -- > original string It will cancel the function of all escape characters in it
The escape character symbol is Don't mix it up with / about this
Here is a website to help you learn
http://c.biancheng.net/view/2176.html`
notes
notes = Documentation,instructions,Let others see your code,Know what it means? Comments are just for developers to see,It will not participate in the actual operation of the code
1. Single line annotation: use # to annotate the content to be annotated. Operation method – > # content (Shortcut ctrl + / for the content you want to comment)
name = 'Xiaohong is great' #Comments on Xiao Hong
2. Multiline comment: content enclosed in three pairs of single or double quotation marks
'''
Note Content
'''
"""
Note Content
"""
There is another use of comments, one line / several lines of code I don't want it to run, but I don't want to delete it Comment it out
Multiline comment -- > a three quote string that is not used
''' Comments on Xiao Hong ''' name = 'Xiaohong is great' print(name)
Nomenclature
Many large companies have their own naming conventions At present, the two commonly used naming methods are:
1. Hump naming method: capitalize the first letter of the word to improve code reading
Username -- > user name
Usermoney -- > user amount
2. Hungarian nomenclature: it is mainly used in later development, and it is not required to master it in the learning stage
Data type, scope and variable names are separated by underscores_
g_ Iuserage -- > User age of global int type
G = global -- > Global
I = int -- > integer
Userage -- > variable name, user age