python String (String)

'' 'What is the string 
an arbitrary text string is a single or double quotes
' ABC '
"DEF"
string immutable
' ''

# create a string
str1 = "sunck IS A Good man!"
Str3 = "Nice sunck IS A man!"
str5 = "sunck IS A Handsome man!"

'' '
string operations
' ''
# string concatenation
STR6 = "sunck IS A"
STR7 = "Good man"
Str8 + = STR6 STR7
Print ( "STR6 =", STR6)
Print ( "STR7 =", STR7)
Print ( "Str8 =", Str8)
# repeated string output
STR9 = "Good"
str10 = STR9. 3 *
Print ( "str10 =",str10)
# access the string of a single character
# to locate the character indexed by the subscript, the index starts at 0
# string name [subscript]
STr11 = "sunck IS A Good man! Kay"
Print (STr11 [1])
# str11 [1] = "a" # string immutable
Print ( "STr11 =", STr11)

# string taken part
str13 = "sunck IS A Good man!"
# taken from the start to set a mark to set a standard prior
str15 str13 = [6:15]
# taken prior to re-set standard
str16 str13 = [:. 5]
# to set the scale taken at the beginning to the end
str17 = str13 [16:]
Print ( "str17 =", str17)

str18 = "A Good man IS sunck "
Print (" Good "in str18)
Print (" good1 "Not in str18)

#Print (~. 5) #. 6
'' '
00000101
11.11101 million
10000110
' ''

# formatted output
Print (" A Good man SUNC IS ")
NUM 10 =
str19 = "Nice sunck IS A man!"
F = 10.1267
Print ( "NUM =", NUM, "str19 =",str19)
#% F% S% D placeholder
# 3 decimal place, are rounded
Print ( "NUM =% D, str19 =% S, F% = .3f"% (NUM, str19, F))

'' '
escape character \
convert some of the characters into characters have special meaning
' ''
# \ n-
Print ( "NUM =% D \ nstr19 =% S \ =% NF .3f"% (NUM, str19, F))
'' '
\\
' ''
Print ( "\\ sunck IS")

# \ '\ "
Print ( 'Tom IS A \' Good \ 'man')
Print (" Tom IS A \ "Good \" man ")
# Tom IS A 'Good' man

# If a lot of wrapping the strings, with \ n write reading in a row not
Print ( "good \ nnice \ nhandsome")
Print ( '' '
good
Nice
Handsome
' '')

'' '
\ tab T
' ''
Print ( "sunck \ tgood")


# If there is a lot of character strings need to escape, it is necessary to add a lot of \, for simplicity, Python allows r represents the inside of the default character string is not escape
# \\\ t \\
print (r "\\\ t \\" )
Print (R & lt "C: \ the Users \ XLG \ Desktop \ the Python-1704 \ day03")
Print ( "C: \\ \\ Desktop XLG the Users \\ \\ \\ day03 the Python-1704")
'' '
Windows
C: XLG the Users \\ \\ Desktop \\ \\ \\ day03 the Python-1704
Linux
/ the root / User / sunck / Desktop / the Python-1704 / day03
'' '


#eval (str)
# function: as an effective string str expression is evaluated and computed results back
num1 = the eval ( "123")
Print (num1)
Print (type (num1))
Print (the eval ( "123 +"))
Print (the eval ( "- 123"))
Print ( the eval ( "12 is +. 3"))
Print (the eval ( "12-3"))
#Print (the eval ( "A123"))
#Print (the eval ( "12a3"))


#len (STR)
# returns a string length (number of characters)
Print (len ( "Good sunck IS A man Kai"))

#Lower () converts a string uppercase to lowercase
str20 = "SUNCK is a Good Man ! Kai"
= str20.lower str21 ()
Print (str21)
Print ( "% S STR20 ="% (STR20))

#upper () converts the string of small letters to capital letters
str21 = "A Good Man SUNCK IS!"
Print (str21 .upper ())


#swapcase () converts the string to uppercase letters lowercase letters, uppercase letters to lowercase letters
str22 = "SUNCK iS A GOOD mAn!"
Print (str22.swapcase ())


#capitalize () first letter capitalized, other lowercase
str23 = "SUNCK iS a GOOD mAn!"
Print (str23.capitalize ())


initials #title () every single uppercase
str24 = "SUNCK iS a GOOD mAn!"
Print (str24.title ())

# char Character
#center (width [, FillChar])
# returns a string that specifies the width of the center, as a string filled FillChar default spaces filled
str25 = "Nice kaige iS a man"
Print (str25.center (40, "* "))


#ljust (width [,FillChar])
# Returns a string that specifies the width of the left-justified, fllchar to fill characters, a space filled with a default
= str26 "Nice kaige IS A man"
Print (str26.ljust (40, "%"))



#rjust (width [, FillChar])
# Returns a string that specifies the width of the right-aligned, fllchar to fill characters, a space filled with a default
= str27 "Nice kaige iS a man"
Print (str26.rjust (40, "%"))


#zfill (width)
# returns a string of length to width, the original string right alignment, front fill 0
str28 = "kaige a Nice man iS "
Print (str28.zfill (40))

#COUNT (STR [, Start] [, End])
# returns the number of times a string appears strc, a range can be specified, the default start to finish
str29 =" kaige A Very Very Nice man IS "
Print (str29.count (" Very ", 15, len (str29)))


#find (str [, Start] [, End])
# from left to right is included in the character string detection str the string, the specified range, the default start to finish. Obtained is the first time the start index, without return -1
str30 = "A Very Very Nice kaige IS man"
Print (str30.find ( "Very"

Print (str30.find ( "Very", 15, len (str30)))


#rfind (STR [, Start] [, End])]
str30 = "A Very Very Nice kaige IS man"
Print (str30.rfind ( " Very "))
#Print (str30.rfind (" Good "))
Print (str30.rfind (" Very ", 0, 15))

#index (STR, Start = 0, End = len (STR))
# find root () the same, but if there is no time to return str an exception
str31 = "kaige iS a Very Very Nice man"
#Print (str31.index ( "Good"))


#rindex (str, Start = 0, End = len (str))
# root rfind () the same, but if there is no time to return str an exception
str32 = "kaige iS a Very Very Nice man"
Print (str32.rindex ( "Very"))


#lstrip () amputated the left side of the character string specified, the default is a space
str33 = "******* kaige iS a Nice man"
Print (str33.lstrip ( "*"))

#Rstrip () truncated right specified character string, the default is a space
str34 = "kaige is a nice man "
print(str34.rstrip(), "*")

str35 = "*******kaige is a nice man*********"
print(str35.strip("*"))


str36 = "a"
print(ord(str36))
str37 = chr(65)
print(str37)

print(" " != " ")









Guess you like

Origin www.cnblogs.com/pygo/p/12188045.html