As long as Python is a quoted string is
""" """ ''' ''' '' '' ' '
Each letter string or character element is called
Index (subscript)
"meet"
0123
Row from left to right
"meet"
-4-3-2-1
Row from left to right
a = "meet"
print (a [4]) - Output a fourth index -a number of elements is "0123" at position 4 is not so, the output will be given (not exceed the maximum value of the index when the index)
print(a[0]) == print(a[-4])
print(a[1]) == print(a[-3])
print(a[2]) == print(a[-2])
print(a[3]) == print(a[-1])
print (a [1: 3]) [index] [starting position (comprising): end position (not included)] ------------ care regardless tail
print (a [1:]) [1 (starting position) :( default to the last)]
print (a [:]) [(default from the beginning) :( default to the last)]
print(a[0:-2])
Slice when you can exceed the index value
a = "alex_wu_sir,_tai_bai_日魔"
print(a[1:8:2]) -- lxw_
print (a [1: 8: 4]) - lw
print (a [-12: 3: 2]) - you
print(a[10:-5:1]
print(a[-5:-10:-1])
Slice [Initial value: Termination: step] --------------- search direction decision step
String Usage:
The verification code is converted to uppercase
INPUT = User ( "username:")
pwd = INPUT ( "password:")
YZM = INPUT ( "Please enter the verification code (A0oE):")
yzm.upper IF () == "A0oE" .upper ():
IF the User == 'alex' and pwd == "alexdsb":
Print ( "Login successful!")
the else:
Print ( "account number or password is wrong!" )
the else:
Print ( "code error!")
Strings are immutable data type, the string is ordered
All converted to uppercase upper
= name "Alix"
A = name.upper () # all uppercase
print (a) ------ output is Alix
Print (name) are outputted ALIX -----
All converted to lowercase .lowe
= name "ALEX"
A = name.lower () # all lowercase
print (name) ------ output is ALEX
Print (A) is outputted alix -----
Check whether start with ... startswith
= name "Alix"
Print (name.startswith ( 'i', 2,3)) to the beginning of what # - returns a Boolean value - the 2_3-th position is not beginning with i
print (name.endswith ( 'l ', 0,2)) # what end - returns a Boolean value - 0_2 at the end position is not in the l
Statistics count count
= name "alexwusirtaibIa"
Print (name.count ( "i")) count the number of times i appearing in alexwusirtaibIa ------
name = "aleX leNb" 指定前四位出现的次数
a = name.count("l",0,3)
print(a)
Strips spaces across the strip
user = input("username:").strip()
the INPUT = pwd ( "password:"). Strip ()
IF the User == 'alex' and pwd == "alexdsb":
Print ( "Login successful!")
the else:
Print ( "account number or password is wrong!")
= pwd "alexdsb"
A = pwd.strip () # default off off (off the head and tail ends of the space, newline \ n-, tab \ T)
Print (A)
pwd = "alexasdsbal"
A = pwd.strip ( "al") # remove head and tail ends content specified
print (a)
Separate split
= name "alex_wu_si_r"
A = name.split ( "_") # split (space by default, newline \ n-, tab \ T)
Print (A) # [ 'Alex', 'wusir']
print (name.split ( "_", 2)) # can specify the number of times of division
= name "Alex, wusir, TA, I, B, A, I"
A = name.replace replaced ( ",", ".") # all replaced with a comma period all
print (a)
a = name.replace ( ",", ".", 4) # alternative replacement frequency can be specified with a period four times commas
print (a)
Format string (string methods) the format
name = "{} year: {}". format ( "Yuan Bao", 18) is filled by ordinal position #
print (name)
name = "{} year. 1: {0}". format ( " Yuan Bao", 18) is filled by index #
print (name)
name = "{name} year: {age}". format ( name = " Yuan Bao", age = 18) # filled by name
print (name)
There are three strings:% sf format
It is a series of judgment returns a Boolean value
Msg = "alix"
Print (msg.isdigit ()) determines the contents of the string is not all digital (Arabic numerals)
print (msg.isdecimal () judgment is not a decimal number
print (msg. (isalnum ()) is determined not alphanumerics Chinese
print (msg. (isalpha ()) is determined is not a letter, Chinese
FOR usage
msg = "Today is a good day, Cao Yang Sun Devils have to go"
print (len (msg)) --- See msg String length
While loop print string for each element
i = 0
while i < len(msg): # while 0 < 16:
print(msg[i]) # print(msg[0])
i += 1
for loop
for i in msg:
print(i)
for keywords
i variable name (can be modified)
in the keyword
msg iterables
Iterables: Python data types except int, bool iteration can rest
msg = "Today is a good day, Cao Yang Sun Devils have to go"
msg = "abcds"
for a in msg:
print(a)
print(a)
in A for "abcds":
Pass through the # placeholder
print (a)
for i in "dsb":
i = i+"sb"
print(i)