python string notes

String type representation

    Can use single or double quotes (can only represent a single line)

    Triple single quotes or triple double quotes to indicate multi-line data, triple single quotes can also be used as comments

        Role: Redundancy of string representation can handle the case where quotes are in strings.

     the index of the string 

        Forward 0 starts reverse -1 starts

     string slice

        <string>[M:N] from M to N-1

            Where M and N can be missing, M missing means from the beginning, N missing is until the end

        [M:N:K] Slice the string according to the step size K

            Such as "012345678" [1:8:2] the result is "1357"

            Application: <string>[::-1] means reverse order

    Escapes\

        \"Indicates that there is " in the string

        The specific meaning of the escape character in combination with a specific character

        \b fallback \n \r

string operator

Operators and usage describe
x + y concatenate two strings
n*x or x*n Copy the string n times
x in s Returns True if x is a substring of s; otherwise returns False

Input: an integer from 1-7, indicating the day of the week

Output: The week string corresponding to the input integer

For example: input 3, output Wednesday

Solution one:

#WeekNamePrint.py
weekStr = "Monday Tuesday Wednesday Thursday Friday Saturday Saturday"
weekId = eval(input("Please enter the week number (1-7): "))
pos = (weekId - 1) * 3
print(weekStr[pos:pos+3])
Solution two:
WeekStr = "One Two Three Four Five Six"
WeekId = eval(input("Please enter the week number (1-7):"))
print("Week" + WeekStr[WeekId-1])


String handling functions

function and usage describe
len(x)

length, returns the length of the string;

For example, the result of len("one two three 123") is 6

str(x)

the corresponding string form of any type x

str(1.34) results in "1.34", str([1,2]) results in "[1,2]"

hex(x) or oct(x)

string of hexadecimal or octal lowercase form of integer x

The result of hex(2) is "0x1a9"

chr(u) x is Unicode encoding, return its corresponding character
word (x) x is a character, return its corresponding Unicode code

Unicode encoding

  1. Uniform character encoding, which is an encoding method that covers basically all characters
  2. From 0 to 1114111 (0x10ffff) space, each encoding corresponds to a character
  3. Every character in a Python string is a Unicode character

String handling methods

A method refers specifically to a function <b>() in the <a>.<b>() style

method and use describe
str.lower()或str.upper()

Returns a copy of the string, all uppercase or lowercase

For example, ""AbCd".lower() results in "abcd"

str.spilt (sep = None)

Returns a list consisting of parts of str split according to sep

"A,B,C".split(",") results in ['A','B','C']

str.count(sub)

Returns the number of times the string appears in str

"a apple a day".count("a")结果为4

str.replace(old,new)

Returns a copy of the string str, with all old strings replaced with new

"hel".replace("l","llo") results in "hello"

str.strip(chars)

Strip the characters listed in the left and right chars from str

“=python =”.strip(" =p")结果为"python”

str.jojn(iter)

在iter变量除最后元素外每个元素后增加一个str

如“,”.join("12345")结果为“1,2,3,4,5”

str.center(width,fillchar) width代表宽度,fillchar代表填充字符

字符串类型的格式化

字符串格式化使用.format()方法用法如下

<模板字符串>.format(<逗号分隔的参数>)




print("", end="")如果有end=“”表示没有换行

Guess you like

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