python full stack development - Day3 string

python full stack development - Day3 string

1, Expand string learning according to the following points

Chen 1: basic use

1, use

#First of all, the main function of string: name, gender, nationality, address and other description information

2. How to define

In single quotation mark \ double quotation mark \ triple quotation mark, it is composed of a string of characters

 

3. Common operation + built-in method

#Priority operations:

#1. Value by index (forward + reverse): only

#2. Slice (head, tail, step)

#3. Length len

#4. Member operations in and not in

#5. Remove blank strip

#6. split

#7, cycle


#2: Summary of this type
1. Save one value or multiple values
Only one value can be saved
Multiple values can be saved. What kind of values can be
2. Order or disorder
3. Variable or immutable
!!! Variable: the value changes and the id remains unchanged. Variable = = non hash
!!! Immutable: when the value changes, the id changes. Immutable = = hash able

2, Follow the questions above to learn about strings

#Definition: in single quotation mark\Double quotation marks\Three quotes, consisting of a string of characters
name='qianduoduo'
name="qianduoduo"
name='''qianduoduo'''

#Priority operations:
#1,Value by index(Forward fetch+Reverse fetch) : Can only take
name="qianzeliang"
name[0]          #The value is"q"
name[-1]         #The value is"g"


#2,Section(Step length)
name="qianzeliang"
name[2:8]   #The value is " anzeli"
name[2:8:2]   #The value is " azl"
#3,length len
name="qianzeliang"
len(name)     #The value is 11.
#4,Member operation in and not in
name="qianzeliang"
"q" in name   #True
"q" not in name  #False
#5,Remove blank strip
name="  qianzeliang  "
name.strip()    #The value is qianzeliang
#6,segmentation split
name = "qian:ze:liang"
name.split(":")      #The value is["qian","ze","liang"]

#7,loop
msg='qianduoduo'

i=0
while True:
    if i < len(msg):
        print(msg[i])
        i+=1
    else:
        break



#Operations to master

#1,strip,lstrip,rstrip
#2,lower,upper
#3,startswith,endswith
#4,format Three ways to play
#5,split,rsplit
#6,join
#7,replace
#8,isdigit


#strip
name='*egon**'
print(name.strip('*'))
print(name.lstrip('*'))
print(name.rstrip('*'))

#lower,upper
name='egon'
print(name.lower())
print(name.upper())

#startswith,endswith
name='alex_SB'
print(name.endswith('SB'))
print(name.startswith('alex'))

#format Three ways to play
res='{} {} {}'.format('egon',18,'male')
res='{1} {0} {1}'.format('egon',18,'male')
res='{name} {age} {sex}'.format(sex='male',name='egon',age=18)

#split
name='root:x:0:0::/root:/bin/bash'
print(name.split(':')) #The default separator is space
name='C:/a/b/c/d.txt' #Just want to get the top-level catalog
print(name.split('/',1))

name='a|b|c'
print(name.rsplit('|',1)) #Cut from right

#join
tag=' '
print(tag.join(['egon','say','hello','world'])) #Iteratable objects must all be strings

#replace
name='alex say :i have one tesla,my name is alex'
print(name.replace('alex','SB',1))

#isdigit: Can judge bytes and unicode type,Is the most commonly used to determine whether a character is"number"Method
age=input('>>: ')
print(age.isdigit())

Three, summary

1. Only one value can be saved

2, orderly

3. Immutable: when the value changes, the id changes. Immutable = = hash able

 

 

 

 

 

 

 Other operations (just understand)

#1,find,rfind,index,rindex,count
#2,center,ljust,rjust,zfill
#3,expandtabs
#4,captalize,swapcase,title
#5,is Number series
#6,is Other


#find,rfind,index,rindex,count
name='egon say hello'
print(name.find('o',1,3)) #Head and tail,Return if not found-1 No mistake.,Show index if found
# print(name.index('e',2,4)) #Same as above, but no error will be reported
print(name.count('e',1,3)) #Head and tail,Find all if no range is specified

#center,ljust,rjust,zfill
name='egon'
print(name.center(30,'-'))
print(name.ljust(30,'*'))
print(name.rjust(30,'*'))
print(name.zfill(50)) #Fill with 0

#expandtabs
name='egon\thello'
print(name)
print(name.expandtabs(1))

#captalize,swapcase,title
print(name.capitalize()) #title case
print(name.swapcase()) #Flip Case
msg='egon say hi'
print(msg.title()) #Capitalize each word

#is Number series
#stay python3 in
num1=b'4' #bytes
num2=u'4' #unicode,python3 No need to add u Namely unicode
num3='Four' #Chinese numerals
num4='' #Rome digital

#isdigt:bytes,unicode
print(num1.isdigit()) #True
print(num2.isdigit()) #True
print(num3.isdigit()) #False
print(num4.isdigit()) #False

#isdecimal:uncicode
#bytes No type isdecimal Method
print(num2.isdecimal()) #True
print(num3.isdecimal()) #False
print(num4.isdecimal()) #False

#isnumberic:unicode,Chinese numerals,Rome digital
#bytes No type isnumberic Method
print(num2.isnumeric()) #True
print(num3.isnumeric()) #True
print(num4.isnumeric()) #True

#Three cannot judge floating point
num5='4.3'
print(num5.isdigit())
print(num5.isdecimal())
print(num5.isnumeric())
'''
//Conclusion:
    //The most commonly used is isdigit, which can judge the types of bytes and unicode, which is also the most common digital application scenario
    //If you want to judge Chinese numbers or roman numbers, you need to use isnumeric
'''

#is Other
print('===>')
name='egon123'
print(name.isalnum()) #A string consists of letters or numbers
print(name.isalpha()) #A string consists of only letters

print(name.isidentifier())
print(name.islower())
print(name.isupper())
print(name.isspace())
print(name.istitle())

Tags: Python

Posted on Tue, 31 Mar 2020 13:31:05 -0700 by sstangle73