The basic string using python3

'' ' 
A, String 
String is immutable objects, so any operation will not have any impact on the original string. 
Immutable objects There are four: str, int, BOOL, tuple 
1, slice and the index 
1.1 index, the index is down superscript, subscript 0 from the beginning. 
'' ' 
# S1 = "Chinese" 
# Print (len (s1 )) of the length of the string # 2 
# Print (S1 [0]) in # 
# Print (S1 [. 1]) # State 
# Print (S1 [-1]) # State reciprocal of 
# Print (S1 [-2] ) #, the penultimate 

'' ' 
1.2 slice, may be used to intercept the content index of the string. 
syntax: STR [start: end] 
rule: care regardless tail, taken beginning at start, to the end position of interception but not including the End 
'' ' 
S2 = " I want to learn the Python " 
Print (S2 [0: 3])   # acquires from 0 to 3, excluding 3.
# From 2 get to 5, excluding 5 results: learn Py 
Print (s2 [4:])   # from April to the end, the result is: ython 
Print (s2 [:])   # scratch get to the final result: I want to Learn the Python 

Print (S2 [-1: -5])   # acquired from -1 to -5 this is to obtain any results of 
Print (S2 [-5: -1])   # acquired from -5 to -1, the result : ytho 
Print (s2 [-5:])   # get from -5 to final results: ython 
Print (s2 [: - 1])   # this is to get to the last, and result: I want to learn Pytho 

Print (s2 [1: 5: 2])   # from the first beginning to take, get to the fifth, every 2 to take a result: to P 
Print (S2 [: 5: 2])   # from the beginning to the fifth , each take a two results: I learn Y 
Print (S2 [:: 2. 4])   #4 taken from the beginning to the end, each take a two results: yhn 
Print (S2 [-5 :: 2])   # from the penultimate 5 starts to take, get to the end, each of the two take a result: yhn 

Print (S2 [-1: -5: -1])   # countdown from a first to take the reciprocal of the fifth, the values from right to left, the results: noht 
Print (S2 [-5 :: -. 3])   # taken from the beginning to the penultimate 5, taken every 3 a, the result is: y for a 
'' ' 
step: if it is positive, taken from left to right. If it is negative, it is taken from right to left, the default is 1. 
Slice syntax: 
STR [Start: end: STEP] 
Start: start position 
end: end position 
step: step 
'' ' 

' '' 
2, case and turn 
'' ' 
S1 = " Hello World! " 
RET = s1.capitalize ()   # capitalized 
Print (s1)   # the Hello world!
(RET)   # the Hello world! 

RET = s1.lower ()   # all converted to lowercase 
Print (RET)   # the Hello world! 

RET = s1.upper ()   # all converted to uppercase 
Print (RET)   # HELLO WORLD! 

RET = s1 .swapcase ()   # case interchangeable 
Print (RET)   # HELLO WORLD! 

s1 = " the Hello, world " 
RET = s1.title ()   # each separated by a special character letters capitalized 
Print (RET)   # the Hello , World 

'' ' 
. 3, cut to cut 
' '' 
S1= " Chinese " 
RET = s1.center (10, " * " )   # stretched to 10, the intermediate discharge the original string, the remaining positions up * 
Print (RET)   # **** **** China 

S1 = " China \ t good " 
Print (s1)   # Chinese good 
Print (s1.expandtabs ())   # can change the \ length t, change the default length of 8 results: Chinese good 

s1 = " the Hello world " 
RET = s1.strip ( )   # remove space around ends 
Print (RET)   # Hello World 

RET = s1.lstrip ()   #Removing spaces left 
Print (RET)   # Hello World 

RET = s1.rstrip ()   # strips spaces to the right of 
Print (RET)   #   Hello World 

'' ' 
. 4, the replacement string Replace () 
' '' 
S1 = " apple_banana_orange_strawberry " 
RET s1.replace = ( " Orange " , " orange " )
 Print (RET)   # apple_banana_ orange _strawberry 

RET = s1.replace ( " _ " , " # " ,  2)  #To replace _ #, replacing 2 
Print (RET)   # Apple Banana # # orange_strawberry 

'' ' 
. 5, a cutting string Split () 
' '' 
S1 = " apple_banana_orange_strawberry " 
RET = s1.split ( " _ " )
 Print ( RET)   # [ 'Apple', 'Banana', 'Orange', 'Strawberry'] 

'' ' 
. 6, the strings together the Join () 
' '' 
LST = [ ' Apple ' , ' Banana ' , ' Orange ' , 'Strawberry ' ] 
court= ' , ' .Join (LST)
 Print (RET)   # Apple, Banana, Orange, Strawberry 

'' ' 
. 7, startsWith () determines whether the beginning ... 
' '' 
S1 = " Apple " 
RET = s1.startswith ( ' a ' )   # determines whether to begin a 
Print (RET)   # True 

RET = s1.endswith ( ' E ' )   # determines whether to end h 
Print (RET)   # True 

'' ' 
. 8, the count cOUNT () 
' ''
s1 = "Banana " 
RET = s1.count ( " a " )   # lookups occur a 
Print (RET)   # . 3 

'' ' 
. 9, lookup index find () and index () 
' '' 
S1 = " Banana " 
RET = S1. Find ( " a " )   # find a number of index 
Print (RET)   # . 1 
RET = s1.find ( " m " )   # not found, return -1 
Print (RET)   # -1 

RET = s1.index ( "a")   # Find a number of index 
Print (RET)   # . 1 
# RET = s1.index ( "m") # not found error: a ValueError: Not found the substring 

'' ' 
10, isdigit () determines whether a digital 
' '' 
S1 = " 1234 " 
RET = s1.isdigit ()
 Print (RET)   # True 

'' ' 
. 11, the length len of the string () 
' '' 
S1 = " Hello World " 
RET = len (S1)
 Print (RET)   # 11 

'' ' 
12, the string is iterables 
' ''
s1 = "World Hello " 
for I in S1:
     Print (I)
 '' ' 
Print Results: 
H 
E 
L 
L 
O 
 
W 
O 
R & lt 
L 
D 
' ''

 

Guess you like

Origin www.cnblogs.com/lilyxiaoyy/p/11845794.html
Recommended