English character and character detection correlation function
varstr = ' Anything worth doing , Just do it well . whatever is worth doing is worth doing well.'
''' 1. Case detection '''
# str.capitalize() Returns a copy of the original string , Its initials are capitalized , The rest are in lowercase .
# res = varstr.capitalize()
# str.title() Returns a copy of the original character , Capitalize each word in the string
# res = varstr.title()
# str.upper() Convert all English in the string to uppercase
# res = varstr.upper()
# str.lower() Converts all English characters in a string to lowercase
# res = varstr.lower()
# str.swapcase() Returns a copy of the source string , Where uppercase characters are converted to lowercase , vice versa
# res = varstr.swapcase()
''' 2. String detection method '''
# Check whether the English in the current string is all composed of uppercase
# res = varstr.isupper()
# Check whether the English in the current string is all composed of lowercase
# res = varstr.islower()
# Detect whether the current string is composed of characters ( chinese , English characters , Numbers ) form
# res = varstr.isalnum()
# Check whether the current character is in Chinese , English character composition ( Excluding numbers and other characters )
# res = varstr.isalpha()
# Check whether the current character is composed of numbers
# res = varstr.isdigit()
# Detect whether the current character is composed of space characters
# res = varstr.isspace()
# Detects whether a string starts with a specified character , You can specify the start and end positions
# res = varstr.startswith(‘l’,1)
# Detects whether a string ends with a specified character , You can specify the start and end positions
# res = varstr.endswith(‘l’,1)
print(res)
String lookup and operation related functions !!!
vars = 'If you wish to succeed, you should use persistence as your good friend, experience as your reference, prudence as your brother and hope as your sentry.' \
''' String search '''
# Detect whether one string exists in another string
# res = 'you' in vars
# Gets the length of the string
# res = len(vars)
# str.fin(sub[,start[,end]]) # Gets the index position of the first occurrence in the specified string from left to right , Return if not found -1
# res = vars.find('you')
# res = vars.find('you',10,34)
# str.rfin(sub[,start[,end]])
# # Gets the index position of the first occurrence in the specified string from right to left , Return if not found -1
# res = vars.rfind('you',10,34)
# str.index() and find() The method is the same , If we don't find it, we'll report an error
# res = vars.index('you')
# str.rindex() and find() The method is the same , If we don't find it, we'll report an error
# res = vars.rindex('you')
# str.count(sub[,start[,end]])
# Count the number of times a character appears in a string
res = vars.count('you')
''' String manipulation related functions '''
print(res)
Data type details
Definition of list
You can use brackets to define []
have access to list Function definition
When defining the elements of a list , You need to use commas... Between each element , separation .[1,2,3,4,5,6]
The elements in the list can be of any type , Usually a collection of items of the same kind
Basic operations for lists
List definition
List together
List splicing
List multiply
Subscript of list — Get updates
Addition of list elements
Deletion of list elements
varlist1 = [1,2,3,4,5,6]
varlist2 = ['q','w','e','r','t']
# Splicing of lists . Put several list elements together to form a list
# res = varlist1 + varlist2 + [11,22,33]
# Repetition of list elements
# res = varlist1 * 4
# Check whether the element exists in the list
# res = 'a' in varlist2
# Index operation of list
''' 0 1 2 3 'q','w','e','r' -4 -3 -2 -1 '''
# res = varlist2[2]
# Modify the elements through the following table
# res = varlist2[2] = 'ee'
# Cannot add elements by subscript
# Append elements to the list
# varlist2.append('y')
# varlist2.append(['yy','uu','ii','oo','pp'])
# Get list length
# res = len(varlist2)
# Deletion of list elements , Delete elements by subscript 、
# del varlist2[2]
print(varlist2)
# print(res)
Slices in the list
varlist = [' Lau Andy ',' Leslie cheung ',' Aaron Kwok ',' Little Shenyang ',' Lennon ',' Song Xiaobao ',' Xiaohua ',' Xiao Zhang ',' petty thief ',' Xiao Su ']
''' grammar ==> list [ Start index : End index : Step value ] 1. list [ Start index :] ==> From the beginning of the index to the end of the list 2. list [: End index ] ==> From start to before the specified index 3. list [ Start index : End index ] ==> From the beginning of index to the end of index 4. list [ Start index : End index : Step value ] ==> From the specified index to the specified index , Slice the values according to the specified steps 5. list [::] list [:] Slices of all elements 6. list [::-1] It's getting data backwards '''
# res = varlist[2:] # From the beginning of the index to the end of the list
# res = varlist[:2] # From start to before the specified index
# res = varlist[2:8] # From the beginning of index to the end of index
# [' Aaron Kwok ', ' Little Shenyang ', ' Lennon ', ' Song Xiaobao ', ' Xiaohua ', ' Xiao Zhang ']
# res = varlist[2:8:2] # From the specified index to the specified index , Slice the values according to the specified steps
# [' Aaron Kwok ', ' Lennon ', ' Xiaohua ']
# res = varlist[:]
# Use the slicing method , Update and delete list data
# Start with the specified subscript , End before the specified subscript , And replace with the corresponding data ( Container type data , Will be split into each element for assignment )
# varlist[2:4] = '1'
# varlist[2:5] = 'wer'
# varlist[3:5] = ['w','r','d']
varlist[2:3:4] = ['w'] # Be the same as the number of elements to be updated
# Slice delete
del varlist[2:3:4]
del varlist[2:3]
print(varlist)
# print(res)
List related functions
# varlist = [' Lau Andy ',' Leslie cheung ',' Aaron Kwok ',' Little Shenyang ',' Lennon ',' Song Xiaobao ',' Xiaohua ',' Xiao Zhang ',' petty thief ',' Xiao Su ']
# len() Detect the length of the current list , The number of elements in the list
# res = len(varlist)
# count() Detects the number of occurrences of the specified element in the current list
# res = varlist.count(' Lau Andy ')
# append() Append new elements to the end of the list , The return value is none
# res = varlist.append(' Small year ')
# insert() You can add new elements to the index location specified in the list
# varlist.insert(1,'dd')
# print(varlist)
# pop() You can perform stack operations on elements at a specified location , Return out of stack elements
# res = varlist.pop() # By default, the last element of the list is pushed out of the stack
varlist = [1,2,3,4,5,6,7,8,9,10]
# remove() You can specify the elements in the list , To delete , Delete only the first , If not found , False report
# res = varlist.remove(1)
# index() You can find the index location where the specified element first appears in the list
# res = varlist.index(1)
# res = varlist.index(1,4,6) # You can find the index location of the element within the specified index range
# extend()
# res = varlist.extend(['w','t'])
# print(res)
# clear() Empty the list
# res = varlist.clear()
# reverse() List inversion
# res = varlist.reverse()
# sort() Sort list
# res = varlist.sort() # Sort by default from large to small
# copy() Copy , Copy the current list
res = varlist.copy()
# Yes copy The list after
del res[4]
print(res)