python_string_learning

# author: Roy.G
a="my name is \t roy.guo,i am {year} years old,i like {frute}"
print(a.center(60,"-")) # .center print 50 character,center the string and fill the gap with -
print(a.capitalize()) #change the tring first letter whth capital letter.
print(a.count("o")) #count the letter or string
print(a.endswith("oy")) #determine if the string ends with an “oy”
print(a.expandtabs(30)) # define the length of the "/t"
print(a.find("roy.guo")) # beginning index of the "roy.guo"
print(a.format(year="twenty-six",frute="appl")) # specifies the value of {}
print(a.format_map({"year":"23","frute":"orange"})) #make a map to find match the {}
print("ab123".isalnum())
print("aba".isalpha())
print("100".isdigit())
print("200".isdecimal())
print("0a100".isidentifier())
print("33.33".isnumeric())
print("My Name Is Roy".istitle())
print("+".join(["1","2","3","4"]))
print("\nroy")
print("\nroy".lstrip())
print("\nroy\n".rstrip())
print("\nroy".strip()) # strip the \n and space
p=str.maketrans("abcdefg","1234567")
pa="my name is abc".translate(p)
print("my name is abc".translate(p)) #translate character to character
pt=str.maketrans("1234567","abcdefg")
pb=pa.translate(pt)
print(pb) #swap the code to decode
print("roy".replace("r","R"))
print("r my name is roy".rfind("r")) #find the rightmost lettter "r"
print("r my name is roy".split("r")) #split string by "r" to a list
print("1+2+\n3+4".splitlines())
print("r my name is roy".title())

posted on 2021-12-26 00:27  ttm6489  阅读(39)  评论(0)    收藏  举报

导航