current position:Home>String and related methods of Python data type introduction

String and related methods of Python data type introduction

2022-01-29 21:17:52 Mr. forgets his name

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .

1、python String type of , keyword str; Definition : Pairs of single and double quotation marks enclose the string

Be careful :python Define variables in , Do not need to use variable type keywords :str character string int integer float floating-point tuple Tuples dict Dictionaries list list

str_2="lemon python class "
 Copy code 

2、 The value of the string , He is orderly , A string is composed of elements one by one , And there's an index index, from 0 Angle mark start number , It also supports reverse order from -1 Start

print(" Take the value of string variable :",str_2)
print(" Positive values :",str_2[2]," Reverse value :",str_2[-16])
 Copy code 

3、 Special characters \n \t \r, escape Add... Before the string r/R Or backslash \, After the transfer is the ordinary string output \n

 Copy code 

4、 String operations + Splicing ,* ride . priority ; Support the same type + Splicing : Different types of splicing , Need to transform , Compulsive . That is, the original type is forced to be spliced with the same type

# print(str_2+str_1*4)
# i=(1,2,3)
# j=111
# print(str(j)+str_1)
# print(str(i)+str_1)
 Copy code 

5、 member operator :in not in

print(" member operator , conditional :",'i' not in str_2,'o' not in  str_2)
 Copy code 

6、 Slice value m:n:k The starting position : Target location : step ( Isochromatic ) The default is 1, Take left instead of right n-1 End value

# [:]  Take it from head to tail ,[2:] from 2 Start taking to the end  [::2] from 0 Start , The isochron is 2 Take to the tail ,[:7:] from 0 Fetch 7 The isochron is 1
print(" Slice value :",str_2[1:16:3],", Support reverse order slicing :",str_2[-16:-3:1]) #  The reverse order is [-16:] Tolerable 
# print(str_2[::2])
 Copy code 

7、 Format output ,%s character string ( You can put any type of value ) %d Integers %f Floating point numbers ,%.2f Keep two decimal places

# %() Coercive transformation , commas , The order value is 
# format {} The space occupation can not mark the position , The default order value is ,index from 0 Start , If the index value , Just mark by index 
print(" Format output :{1}{0}".format(age,name),", Place holder :%s Output %s:"%(str_2,age))
 Copy code 

8、 Use of built-in functions

#  Case Toggle :upper()、lower()
print(" Capitalization :",str_2.upper()," A lowercase letter :",str_1.lower())
# swapcase() Capitalize interchangeably 
# find() Look for the element 
print(" Find the index of the element index:",str_2.find("t"),", Element index not found index Default return :",str_1.find("3"))
#  Substitution function replace(), Be replaced , Replacement value , Specify how many times to replace 
print(str_1," Comparison before and after string element replacement :",str_1.replace("t","T",2))
# split()  function , Cut string , Return list type , The type of list element is still string 
print(str_2," Comparison of string elements before and after cutting :",str_2.split("n"),", Type after cutting :",type(str_4),
      ", But the elements inside \'{}\' Type or string :".format(str_4[2]),type(str_4[2]))
# strip() Handle the character elements at the beginning and end 
print(str_2," Comparison of string elements before and after processing :",str_2.strip("l"))
#  Length function __len__()
print(" Return string str_2 The length of :",str_2.__len__())
 Copy code 

copyright notice
author[Mr. forgets his name],Please bring the original link to reprint, thank you.

Random recommended