Introduction to basic data types: the magic of string transformation

1. Previously learned (numbers, strings, Boolean values)

# python3 All numbers are int type, string type is str type, list is list type, tuples are tuple type, dictionary is dict type, boolean is bool type

#Number (int) corresponding magic:

a1 = "123"
 b = int (a1)            #string is converted into a number 
b = b + 100 
print (b)

 

num = "a"
 v = int (num, base = 16) #Convert      a to a hexadecimal number, the output result is 10 
print (v)

  

age = 10
 r = age.bit_length () #The     binary number of this number is expressed by several bits. The corresponding binary is 1010, which is four bits, so the output result is 4 (at least a few bits to represent) 
print (r)

  

#String corresponding magic

 

test = "alex"
 v = test.capitalize () #The      function result of capitalizing the first letter of the string is Alex 
print (v)
test = "aLeX" 
v1 = test.lower ()
print (v1)
v2 = test.casefold () #The results of both are lowercase, but the difference is that the lower can only change the English, casefold is more cattle You can change some French and German to
print (v2)
test = "aLeX" 
v = test.center (20) # means expand to 20 digits and center the string
print (v)
v2 = test.center (20, "!") # The following things are optional, representing What to fill in spaces, but only one character
print (v2)

test = "aLexalex" 
v = test.count ("e") #count is used to find the number of subsequences in the string, the output is 2
print (v)
v2 = test.count ("ex") #find for ex Number, the output result is also 2
print (v2)
v3 = test.count ("ex", 5) #Find the number of ex from the fifth digit, the output result is 1
print (v3)
v4 = test.count ("ex", 5,6) #Find the number of ex from the 5th to 6th digits, the output result is 0.
print (v4)

encode and decode functions are also very important.

test = "aLexalex" 
v = test.endswith ("ex") #Determine whether the string ends with ex and the output is true
b = test.startswith ("q") #Determine whether it starts with q and the result is false
print (v, b)

test = "alexalex" 
v = test.find ('ex') #find the position of ex
print (v)
v1 = test.find ('ex', 5,8) #find from the 5th to 8th, from [5 -8) Find the position, close left and right, if not found, the return value is -1
print (v1)

test = "i am {name}, age {a}" 
print (test)
v = test.format (name = "alex", a = 15) #Assignment function, assign the things in the braces above to
print (v)

test = "i am {0}, age {1}"
print (test)
v = test. format ("alex", 15) #The upper curly brackets are 0 and 1, and the things in the lower brackets are also assignments. Sequence, separated by comma
print (v)

test = "i am {name}, age {a}" 
v1 = test. format_map ({"name": "alex", "a": 19}) #Format, the incoming value is in the form of a dictionary One corresponding
print (v1)

test = "zxce_324" 
v = test.isalnum () #Determine whether the string contains only numbers and letters. The output result is false because there is an underscore
print (v)
 
 
test = "username \ temail \ tpassword \ nyxz \ [email protected] \ t123 \ n" 
r = test.expandtabs (20) #There is '\ t' in the string, enter this statement to set the distance of this space Set it out, note that the content before the tab is 20 spaces! ! !
print (r) # username is eight, you need to add 12 additional spaces into 20 (the following example, points out the table, and the distance between the headers are all equal)
output results:

                             username      email       password
                              yxz        [email protected]      123

 

test = "asdas123" 
v = test.isalpha () #judgment whether it is a letter, Chinese character
print (v)
test = "213" 
v1 = test.isdecimal () #judgment whether it is a number, but can only be a decimal decimal
v2 = test.isdigit () #judgment whether it is a number, including strong numbers, such as ①, also a number, recognition Come out
v2 = test.isnumeric () #judgment whether it is a number, more powerful, Chinese can recognize
print(v1,v2)

a = "_ 231asd" 
v = a.isidentifier () #Determine whether it is an identifier
print (v)

test = "asdasfgds" 
v = test.isprintable ()
print (v)
test = "asdasfg \ nds"
v = test.isprintable () #Contains some invisible and undisplayable things such as line breaks, tabs, etc. Print, the output is false
print (v)
test = "saddas" 
v = test.isspace () # judge whether all are spaces, the output of this question is false
print (v)

test = "Rsad WR asd" 
v = test.istitle () #Judge whether it is a title, that is, the first letter of each word must be capitalized
print (v)
v1 = v = test.title () #Convert the string Call the title
print (v1)
####important! ! ! Separate each element in the string according to the specified delimiter. This must remember 
test = "You are the wind and I am sand"
print (test) #The output result is that you are the wind and I am sand
= "="
v = t.join (test) #Separate the string with spaces. You are the wind. I am sand.
print (v)
v1 = "_". join (test) # can also be written in this format. Underscore divides you_ Yes_wind_ 儿 _ 我 _ 是 _ 沙
print (v1)
test = "alex" 
v = test.ljust (20, "*") #Adjust the length to 20, put the original on the left and make up what you want
print (v) #The output is alex **** ************
v1 = test.rjust (20, "&")
print (v1) #The output is &&&&&&&&&&&&&&&&& alex
test = "alex" 
v = test.lstrip () #Remove the space on the left and the newline character
v1 = test.strip () #Remove both the left and right sides
v2 = test.lstrip ("ale") #Remove the things specified on the left, Here will output a x
print (v)
print (v1)
print (v2)

test = "You are the wind and I am the sand" 
test1 = "Go to your mother ’s wind and sand"
m = str.maketrans ("You are the wind and I am the sand", "Go to your mother ’s wind and sand") #Create more than one Correspondence between two strings of the same length
v = test.translate (m) #Convert the first string test according to the translation rules
print (v)

test = "testsadasdvxc" 
v = test.partition ("s") #Split the string according to "s", and stop after finding the first s, divided into three parts
print (v) # ('te', 's ',' tsadasdvxc ')
v1 = test.rpartition ("s") #Split the string from the right
print (v1) # (' testsada ',' s', 'dvxc')
v2 = test.split ("s" )
#Separate all the strings, but the middle S is removed print (v2) # ['te', 't', 'ada', 'dvxc']
v3 = test.split ("s", 2) #Split the string, remove s, but only recognize the two, and the next one will not be split
print (v3) # ['te', 't', 'adasdvxc']

# ### 应用 : Calculator
# # Regular expression to determine whether you want to split the element
# "1 * 2" for splitting
# 1 * 2 The first split used
# 1 2 The second split used

test = "testsa \ nda \ nsdvxc" 
v = test.splitlines () #Split according to newline characters and get the result ['testsa', 'da', 'sdvxc']
print (v)
v1 = test.splitlines (True) # Is also divided according to the newline character, but if a true is added, the newline character must be retained, and the output result is ['testsa \ n', 'da \ n', 'sdvxc']
print (v1)

test = "bakwre .1.1." 
v = test.startswith ("bak") #Determine whether the string starts with bak and the output is true
v2 = test.endswith ("213")
print (v)
print (v2)

test = "safBBJkk" 
v = test.swapcase () #case conversion
print (v)


 Six basic functions: join split find strip upper lower (must remember function)

 

The gray magic of strings must also be remembered (test [], len ())

test = "alex"
 v = test [0] #Get a character in the string by index 
print (v) v1 = test [0: 1] #Greater than or equal to 0 and less than 1 range 
print (v1) v2 = test [0: -1] # -1 represents the last digit, here represents the characters greater than or equal to 0 and less than 3 
print (v2) v3 = len (test) #output the length of the current string, in Python, to Zhuo only Three characters 
print (v3) 
li = [11,22,3,34] v4 = len (li) #output     the number of elements in the list, here the output is 4 
print (v4)





test = "要向 卓哈 哈哈 哈哈 哈哈" #One         word output
index = 0
while index <len (test):
print (test [index])
index + = 1
#for loop, for variable name in string (can also be used in the index, can also be used in the slice) 
# print a
for a in test: # can also achieve the effect of output one by one
print (a)
# replace's function replacement 
test = "asdasdqwe"
v = test.replace ("asd", "fgh") #The front is the old string, followed by the new string
print (v)
v1 = test.replace ("asd "," fgh ", 1) #Replace only the first
print (v1)


 Supplementary knowledge: Once a string is created, it cannot be modified. Once modified or spliced, a string will be regenerated in memory. This is the basic principle of the computer.

 

test = "To Xiang Zhuo is really awesome" 
for item in test: 
    print (item) # output the content of the string one by one, in the for loop, break and continue also apply 
#range can help us create continuous numbers, Write 100 in brackets, the default is [0-100] 
# You can also create discontinuous numbers, you need to set the cloth length in brackets v = range (0, 100, 5) v = range (100) 
for a in v: #Create A range, within this range for loop 
    print (a)

 

An exercise question added by the teacher:

Exercises to realize the function: the user enters a string, and outputs each character in the string and the corresponding position. 
My answer:
test = input ("Please enter a sentence:") 
print (test) 
l = len (test) 
print (l) 
r = range (0, l) 
for i in r: 
    print (i, test [i])

 Teacher's simple answer:

 
test = input ("Please enter a sentence:") 
for item in range (0, len (test)): 
    print (item, test [item])
 

  

Guess you like

Origin www.cnblogs.com/yxzymz/p/12709567.html