python notes-----string operations

A string that defines a variable in python

str1 = "www" #str1 is a string, must use quotation marks

Or directly use "string." to call the internal method

1. String case operation

capitalize() method - capitalize at the beginning

print('wsy www'.capitalize())

Wsy www

swapcase() method - all caps

print("wsy ada".swapcase())

WSY ADA

isupper() method - return True or Flase to determine whether all uppercase is uppercase

print ( 'AAA' .isupper()) #Whether they are all uppercase

True

islower() method - to determine whether all lowercase returns True or Flase

print('a,1,a'.islower())

True

upper() method - all uppercase

print('wSy'.upper())

WSY

lower() method - all lowercase

print('wSy'.lower())

wsy

title() method - space-separated first letters are capitalized

print('wsy ww'.title())

Wsy Ww

istitle() method - determine whether the first letter is capitalized by space separation

print('My Name Is '.istitle())

True

replace() method - replace lowercase with uppercase, replace x

print ( 'wsy' .replace( 's' , 'S' , 1 ))    #replace       one

wSy

2. Passing parameters

format() format_map() parameter

name = "wsy  www {w} {s}"
print(name.format(w="1",s=11))

print(name.format_map({'w':'1','s':'11'}))  

wsy  www 1 11

3. Print, complete, convert

count() prints the number of specified characters

print('name'.count("n"))

1

center() prints x characters that are not enough to complete with y, and the characters are centered

print('name'.center(50,"-"))

-----------------------name-----------------------

ljust() prints characters of length x not enough to append with y padding

print ( 'name' .ljust ( 50 , '*' ))

name**********************************************

rjust() prints characters of length x not enough to fill the start with y

print('name'.rjust(50,'*'))

**********************************************name

zfill() prints characters of length x not enough to fill with 0

print('wsywsywsy'.zfill(20))

00000000000wsywsywsy

expandtabs() converts tab keys to x spaces

print ('nam \ te'.expandtabs (tabsize = 30))

for e

find() returns that the starting subscript of the search string is not -1

print('name'.find("na"))

0

rfind() returns the last character subscript of the search string is not -1

print('wsywangsiyu'.rfind('y'))

9

['zifuchuan'.find("a"):] String slice from x to end

print('wsy'['wsy'.find("s"):])

his

join() list to string

print(''.join(['1','2','3','4']))

1234

strip() removes trailing spaces and carriage returns

print('wsy\n'.strip())

wsy

lstrip() removes leading spaces and carriage returns

print('\n   wsy'.lstrip())

wsy

split() is separated into a list according to the specified character, the default space

print ( '.swit .s y' .split ())

print ( 'Ww_sy .ws y' .split ( 's' ))

['ww_sy', '.ws', 'y']

['ww_', 'y .w', ' y']

splitlines() separates a list by newline

print('1234\n 123'.splitlines())

['1234', ' 123']

translate() its own encryption must correspond to the number before and after

p = str.maketrans("abcdef","123456")print("abeeee".translate(p))

125555

4. Judgment

name = 'www'

print(name.endswith('sy')) #Judging       whether the end result of the specified string is true or false
print(name.isalnum()) #Contains           English and Arabic characters without special characters and returns True
print('nameA'.isalpha( )) #Determine           whether it is a pure English character
print(name.isdecimal()) #Whether          it is an integer
print('1A'.isidentifier()) #Determine         whether it is a legal identifier (variable name)
print('0'. isnumeric( )  ) #Determine         whether it is a natural number#
print(' '.isspace()) #Is                it a space
print('My name is '.isprintable()) #Whether              it can print tty drive

False
True
True
False
False
True
True
True

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325275204&siteId=291194637