The string data type (string)

First, the specific character string
1.upper () / .lower () the string into the case, the result is a string obtained
 
 
Example: comparing the input string in lowercase
mode. 1:
check_code = 'AF1k'
new_check_code = check_code.lower ()
Print (new_check_code)
code = 'af1K'
MAG = 'Please enter your PIN% s:'% (code, )
of code1 = INPUT (MAG)
new_code = code1.lower ()
IF new_code == new_check_code:
    Print ( 'authentication success')
the else:
     (' authentication failure ') Print

# mode 2:

check_code =' AF1k '
of code1 = INPUT (' Please enter your verification code S%: '% (check_code,))
IF code1.lower () == check_code.lower ():
    Print (' verification success')
the else:
     Print ( 'authentication failed')



    
 
Whether 2.isdigit () into a digital string is determined, and the results are Boolean True / False
Example: determining whether a digital input string
the while True:
   NUM = INPUT ( 'Enter a number:')
   In Flag = num.isdigit ()
   IF In Flag:
      Print (NUM)
      Print ( 'Input success')
      BREAK
   the else:
      Print (' Please enter the number continues: ')
  
results:  
Please enter numbers: d
Please continue to enter numbers:
Enter numbers: f
proceed to enter numbers:
Enter a number: 1
1
Input success 
 
3.strip () to remove the clear space / .lstrip () to remove the space left / .rstrip () remove the right box
Example: remove whitespace
方式1:
name = ' sundy '
name1 = name.rstrip()
name2 = name1.lstrip()
name3 = name2.upper()
print('-->',name3,'<--')
result:
--> SUNDY <--
方式2:
name = ' sundy '
name1 = name.strip()
print('-->',name1,'<--')
result:
--> sundy <--
 
 
 
4.replace ( "replaced character / sequence", "to be replaced with the content") / .replace ( "replaced character / sequence", "to replace the contents of" 1) from left to right a total of one alternative is to specify alternative content
 
 
Example: Alternative keyword
name = input ( 'Please enter your name:')
NAME1 = name.replace ( "country", '*')
NAME2 = name.replace ( "country", '*', 2)
Print ( '->', NAME1, '<-')
Print ( '->', NAME2, '<-')
result:
Please enter your name: the International Day United States
-> * * home * occasion to celebrate the United States * <-
-> * celebrate * US international <-

 5.split ( 'according to what division') / .split ( 'segmentation based on something', 1) from left to right starting with the first cutting a total of cutting / rsplit from right to left, the result is a list
Example: segmentation specified character
name = 'who you are, where you come from, you have to go, ha ha'
NAME1 = name.split ( ",")
NAME2 = name.split ( ",", 2)
NAME3 = name.rsplit ( ",", 2)
Print ( '->', NAME1, '<-')
Print ( '->', NAME2, '<-')
Print ( '->', NAME3, ' <- ')
Results:
-> [ 'who you are', 'Where are you from', 'where you are going,' 'ha ha'] <-
-> [ 'who you are', 'Where are you from', 'You to go, ha ha '] <-
-> [' who you are, where you come from, '' where you are going, '' ha ha '] <-
 
Second, other types of public
1.len, calculated length. (String -> calculating the number of characters in the string)
Usage:
NUM = len (value)
Print (NUM)
 
2. index values ​​(0 as a start)
 
v = "oldboy" v1 = v [0] # 0 1 2 3 ... front rearwardly v [0] is the first element represents
v2 = v [-1] # -1 -2 -3 ... from forward
v = "oldboy"

3. Slice (0 as a start)

V1 = V # [2:. 4]
# 2 = <index position <3 (opening section)
# V2 = V [3:. 6]
# V2 = V [3: -1]
# V2 = V [3:] from to take the last three positions
# v2 = v [: - 1 ] taken from the last position to the front, but does not include a final
Example:
= v 'sundy'
v1 = v [1: 3]
v2 = v [1: -1]
v3 = v [0:]
v4 = v [: - 1]
print (v1)
print (v2)
print (v3)
print (v4)
 
 
Result:
D: \ python3.6 \ python3.6.exe D: /python_code/day01/day03.py
un
und
sundy
Sund
 

Example: After taking two characters
# data = input ( 'Enter:')
 Method 1
# V = Data [-2:]
# Print (V)
Second way
# the total_len = len (Data)
# V = Data [total_len- 2: the total_len]
# Print (V)
 Exercises:
######################## requirements: allow users to enter an arbitrary string, calculate how many numbers to get the string after ######## ############################
text = iNPUT ( 'enter the string:')
len_text = len (text)
index = 0
Tol 0 =
the while index <len_text:
    value = text [index]
    FAG = value.isdigit ()
    IF FAG:
        Tol +. 1 =
    index + =. 1
Print (Tol)

Guess you like

Origin www.cnblogs.com/sundy08/p/11791434.html