Article From:https://www.cnblogs.com/songzijian/p/8968280.html

   SeeData type: type

 

a = True
print(a,type(a))

True <class 'bool'>      #True The type is bool

a = 'shfg'
print(a,type(a))

shfg <class 'str'>       #'shfg'The type is a string

a = 5
print(a,type(a))

5 <class 'int'>          #5The type is int

 

  1. Types of basic data

  1.int : Integer type

  2.str : String type stores a small amount of data, which is always quoted by quotation marks.

      For example,’sjdi’,’1651′,'[1, 2, 3]'[‘[1,2,3]’, this is just a string, not a list].

  3.bool : The Boolean value is true and false, and there are only two cases:

      True 1 True, False 0 false

  4.list : The list can be placed in a large number of various types of data, easy to operate, and the list is orderly, with index values.SlicingIt is easy to take value.

      list = [‘String, number, bool, [list], {Dictionary: dict}..]

 

a = input('time')                           #Input time 1995/1/7
print(a)
b = a.split('/')                            #"/" segmentation
print(b,type(b))
c = b[0]                                    #Take the list slice [0]
print(c,type(c))
d = int(c)                                  #Transform into int form
print(d,type(d))

Time 1995/1/7
1995/1/7
['1995', '1', '7'] <class 'list'>           #After "/" division
1995 <class 'str'>                          #Convert into strings after slicing
1995 <class 'int'>

  5.tuple : Tuple is called read-only list, that is, data can be queried, but it can not be modified. Therefore, the slicing operation of strings is also applicable to tuples.

      For example, (1, 2, 3…) (“a”, “B”, “C”)

  6.dict : The dictionary stores data in the form of key values. {key: value} can store large amounts of data, relational data.It is the most flexible built-in data structure type among Python except list. Lists are ordered objects combined, and dictionaries are unordered objects. The difference between the two is that the elements in the dictionary are accessed by keys, rather than by offset.

      For example, dict = {‘name’:’ old boy ‘, #name= old boy.

              ’name’_list(List: [‘Cadet a’, ‘B’,…]

              ’alex(“Name of people) ‘: {‘age’ (age): 20,

                      ’hobby'(Hobby:’old_women’

                      }  #The dictionary corresponds to the dictionary of Alex

              }  #The dictionary corresponds to the dictionary of Dict

  7. set(Set: {‘Xi Xi’,’alex’……) using braces, but the contents are separate elements.

  Two. Interchangeability of basic data

  1. int(Integers) < –> str (string)

    int —> str  str(int)

s = 6
p = str(s)
print(p,type(p))

6 <class 'str'>

      str —> int  int(str)  #Conditions: strings must be made up of all numbers.

s = '124'
p = int(s)
print(p,type(p))

124 <class 'int'>

s = 'sfg'   #The input string must be a number
p = int(s)
print(p,type(p))

ValueError: invalid literal for int() with base 10: 'sfg'
(ValueError:The invalid literal meaning is int (), and the cardinal number is 10.'sfg'

  2. str(String) < —> bool (Bohr)

    1.bool —> str # All cited by quotation marks are string examples:’True”False’

    2.str —> bool The non empty is True, and the empty string is false. #Must be separated

“  ” The difference from “” is that although “no” is written, there is space in it, and the computer will think it is not empty character. There is nothing in it, even space, so the computer thinks it is an empty character.

s = '  '
p = bool(s)
print(p,type(p))

True <class 'bool'>

s = ''
p = bool(s)
print(p,type(p))

False <class 'bool'>

  Three, the common operation method of string

     1.The index of a string

       The index is subscript, that is, the element of the string starts from the first, the initial index is 0, and so on.Intercepts an element of a string to form a new string.

a = 'fiibkdjnbviniuhuhiuw'   #The first element "F" is 0, and so on, the ninth is V.
print(a[9])

v
a = 'ioujhjkenji'
print(a['o'])           #Enter the element "O" to find the subscript

    print(a['o'])       #Report errors
TypeError: string indices must be integers

       2.The slicing of a string

      Slicing is a new string that is intercepted by index (index: index: step length).(The principle is to ignore the buttocks and have nothing to do with the original string. The positive and negative steps of the step determine the direction of the slicing

a = 'fiibkdjnbviniuhuhiuw'
b = a[0:9]   #The ninth element is V, because V does not play the role of ignoring the principle of buttocks. Since the step is not written, the nine letters are written by default from left to right.
print(b)

fiibkdjnb

a = 'fiibkdjnbviniuhuhiuw'
b = a[0:9:2]  #From left to right, the default step length is 1, and the step length is 2.
print(b)

fikjb

print(a[0:])  #The tail of the slice is not written, and the default is printed to the end.
    
fiibkdjnbviniuhuhiuw   #The newly printed slices have nothing to do with the original string.

print(a[0:-1])  #When the tail slice is -1, it represents the last element W in the string.

fiibkdjnbviniuhuhiu #Ignoring the buttocks principle, the last element W was not printed.

print(a[-1:0:-1])  #Reverse plus step length, -1 represents the last W, and 0 represents the first F, which will string the string forward from the back.

wuihuhuinivbnjdkbii  #Regardless of the principle of buttocks, the last element of command print (a[-1:0:-1]) is F, so there is no printing.

View Code

              3.Capitalize the initials and the rest of the letters

          capitalize  **(Importance)

a = 'fiibkdjnbviniuhuhiuw'
s = a.capitalize()
print(s)

Fiibkdjnbviniuhuhiuw  #Only the initials are capitalized and the rest of the letters are small

      4.Center Center (length, ‘default by blank’) * * (Importance)

a = 'fiibkdjnbviniuhuhiuw'
s = a.center(50,'*')
print(s)

***************fiibkdjnbviniuhuhiuw*************** 

       5.Full uppercase: upper, all lowercase: lower * * * * (very important)

a = 'fiibkdjnbviniUHUHiuw'
s = a.upper()
print(s)
x = s.lower()
print(x)

FIIBKDJNBVINIUHUHIUW
fiibkdjnbviniuhuhiuw

 Users may need to enter the verification code after login to the interface, but in order to facilitate users, there will always be “case insensitive”.

code = 'xuem'.upper()  #The verifying code is sent to you and the authentication code is xuem. The following "upper ()" means that the letter of the string is changed to upper case.
a = input('Please enter the verifying code, and do not distinguish case and case:').upper()  #Change the input verifying code to uppercaseif a == code:
    print('Verification success')

     6.Decide what to start with: starts with

       Decide what to end with: ends with

            ***(Very important)

a = 'aifnfnadnkjsni'
print(a.startswith('a'))            #When the judgment begins with a The concept of the whole and individual is "Ai" for a, but for the whole string, "Ai" is a small individual.

True                                #It's true

a = 'aifnfnadnkjsni'
print(a.startswith('n'))            #When the judgment begins with n

False                               #This is a fake

#ends with The same ending is the same

Can be sliced, sliced off by comma

a = 'aifnfnadnkjsni'                  #Detected string
b = a.startswith('ifn',1,4)           #The detection string "IFN" (1, 4) is the slice of the string detected from 1 to 4 (ifnf). Does it start with "IFN"?
print(b,type(b)) 

True <class 'bool'>
#It's true

     7.Case reversal (swapcase) * (importance)

a = 'aifnfNADnkjsni'
s = a.swapcase()
print(s)

AIFNFnadNKJSNI

      8.Capitalize the initial letter of each word in the string (title) * (importance).

a = 'ai fnfs#frn$kjs+ni'   #A word with a variety of separate words in a string.
s = a.title()
print(s)

Ai Fnfs*Frn$Kjs+Ni         #All the initials of each separated word are capitalized

      9.By indexing the elements, index, find, and two can be very important.

# index:By indexing elements, they can be sliced, unable to find, and reported wrong.

# find:Index can be sliced by element, can not be found, return to -1【Only a string]

a = 'fiibkdjnbviniUHUHiuw'            #Retrieved string
print(a.find('a'))                    #Check the element "a"
print(a.find('b',4))                  #The retrieval element "B" ("B", "4)" 4 starts the search element "B" from fourth bits.
print(a.index('b',4))

#print(a.index('a'))                  #If you can't search it, you'll get it wrong

-1                                    #find Cannot retrieve the element "a" to return to "-1"
8                                     #find The location of "B" from the fourth bit
8

      10.Remove the spaces at the front and rear ends, line breaks (\n), tab characters (\t = “Tab” key).

# strip   ***(Very important] you can also remove the elements you want to remove, only ends, and stop when you encounter content that is not cleared.

a = '         fiibkdjnbviniUHUHiuw'
print(a)
print(a.strip())

         fiibkdjnbviniUHUHiuw
fiibkdjnbviniUHUHiuw
a = '\tfiibkdjnbviniUHUHiuw\n'
print(a)
print(a.strip())

    fiibkdjnbviniUHUHiuw     #The front space is due to "\t" (equivalent to the Tab key), followed by line changing because of "\n" (newline).

fiibkdjnbviniUHUHiuw

a = input('enter one user name')
if a == 'Small and tender':
    print('Login success')
else:
    print('Login failure')

Please enter a small and tender username#In front of "small tender" add ""
Login failure#Failure of system recognition

a = input('enter one user name').strip()
if a == 'Small and tender'.strip():
    print('Login success')
else:
    print('Login failure')

Please enter a small and tender usernameLogin success