-
[Python tutorial] 20. String methods코딩/Python 2022. 12. 21. 17:18728x90
string.capitalize()
Converts the first character to upper case
txt = "hello, and welcome to my world." x = txt.capitalize() print (x)
string.casefold()
Converts string into lower case
txt = "Hello, And Welcome To My World!" x = txt.casefold() print(x)
string.center(length, character)
Returns a centered string
txt = "banana" x = txt.center(20) print(x)
string.count(value, start, end)
Returns the number of times a specified value occurs in a string
txt = "I love apples, apple are my favorite fruit" x = txt.count("apple") print(x)
string.encode(encoding=encoding, errors=errors)
Returns an encoded version of the string
txt = "My name is Ståle" x = txt.encode() print(x)
string.endswith(value, start, end)
Returns true if the string ends with the specified value
txt = "Hello, welcome to my world." x = txt.endswith(".") print(x)
string.expandtabs(tabsize)
Sets the tab size of the string
txt = "H\te\tl\tl\to" x = txt.expandtabs(2) print(x)
string.find(value, start, end)
Searches the string for a specified value and returns the position of where it was found
txt = "Hello, welcome to my world." x = txt.find("welcome") print(x)
string.format(value1, value2...)
Formats specified values in a string
txt = "For only {price:.2f} dollars!" print(txt.format(price = 49))
string.index(value, start, end)
Searches the string for a specified value and returns the position of where it was found
txt = "Hello, welcome to my world." x = txt.index("welcome") print(x)
string.isalnum()
Returns True if all characters in the string are alphanumeric
txt = "Company12" x = txt.isalnum() print(x)
string.isalpha()
Returns True if all characters in the string are in the alphabet
txt = "CompanyX" x = txt.isalpha() print(x)
string.isascii()
Returns True if all characters in the string are ascii characters
txt = "Company123" x = txt.isascii() print(x)
string.isdecimal()
Returns True if all characters in the string are decimals
txt = "\u0033" #unicode for 3 x = txt.isdecimal() print(x)
string.isdigit()
Returns True if all characters in the string are digits
txt = "50800" x = txt.isdigit() print(x)
string.isidentifier()
Returns True if the string is an identifier
txt = "Demo" x = txt.isidentifier() print(x)
string.islower()
Returns True if all characters in the string are lower case
txt = "hello world!" x = txt.islower() print(x)
string.isnumeric()
Returns True if all characters in the string are numeric
txt = "565543" x = txt.isnumeric() print(x)
string.isprintable()
Returns True if all characters in the string are printable
txt = "Hello! Are you #1?" x = txt.isprintable() print(x)
string.isspace()
Returns True if all characters in the string are whitespaces
txt = " " x = txt.isspace() print(x)
string.istitle()
txt = "Hello, And Welcome To My World!" x = txt.istitle() print(x)
string.isupper()
Returns True if all characters in the string are upper case
txt = "THIS IS NOW!" x = txt.isupper() print(x)
string.join(iterable)
Converts the elements of an iterable into a string
myTuple = ("John", "Peter", "Vicky") x = "#".join(myTuple) print(x)
string.ljust(length, character)
Returns a left justified version of the string
txt = "banana" x = txt.ljust(20) print(x, "is my favorite fruit.")
string.lower()
Converts a string into lower case
txt = "Hello my FRIENDS" x = txt.lower() print(x)
string.lstrip(characters)
Returns a left trim version of the string
txt = " banana " x = txt.lstrip() print("of all fruits", x, "is my favorite")
string.maketrans(x, y, z)
Returns a translation table to be used in translations
txt = "Hello Sam!" mytable = txt.maketrans("S", "P") print(txt.translate(mytable))
string.partition(value)
Returns a tuple where the string is parted into three parts
txt = "I could eat bananas all day" x = txt.partition("bananas") print(x)
string.replace(oldvalue, newvalue, count)
Returns a string where a specified value is replaced with a specified value
txt = "I like bananas" x = txt.replace("bananas", "apples") print(x)
string.rfind(value, start, end)
Searches the string for a specified value and returns the last position of where it was found
txt = "Mi casa, su casa." x = txt.rfind("casa") print(x)
string.rindex(value, start, end)
Searches the string for a specified value and returns the last position of where it was found
txt = "Mi casa, su casa." x = txt.rindex("casa") print(x)
string.rjust(length, character)
Returns a right justified version of the string
txt = "banana" x = txt.rjust(20) print(x, "is my favorite fruit.")
string.rpartition(value)
Returns a tuple where the string is parted into three parts
txt = "I could eat bananas all day, bananas are my favorite fruit" x = txt.rpartition("bananas") print(x)
string.rsplit(separator, maxsplit)
Splits the string at the specified separator, and returns a list
txt = "apple, banana, cherry" x = txt.rsplit(", ") print(x)
string.rstrip(characters)
Returns a right trim version of the string
txt = " banana " x = txt.rstrip() print("of all fruits", x, "is my favorite")
string.split(separator, maxsplit)
Splits the string at the specified separator, and returns a list
txt = "welcome to the jungle" x = txt.split() print(x)
string.splitlines(keeplinebreaks)
Splits the string at line breaks and returns a list
txt = "Thank you for the music\nWelcome to the jungle" x = txt.splitlines() print(x)
string.startswith(value, start, end)
Returns true if the string starts with the specified value
txt = "Hello, welcome to my world." x = txt.startswith("Hello") print(x)
string.strip(characters)
Returns a trimmed version of the string
txt = " banana " x = txt.strip() print("of all fruits", x, "is my favorite")
string.swapcase()
Swaps cases, lower case becomes upper case and vice versa
txt = "Hello My Name Is PETER" x = txt.swapcase() print(x)
string.title()
Converts the first character of each word to upper case
txt = "Welcome to my world" x = txt.title() print(x)
string.translate(table)
Returns a translated string
#use a dictionary with ascii codes to replace 83 (S) with 80 (P): mydict = {83: 80} txt = "Hello Sam!" print(txt.translate(mydict))
string.upper()
Converts a string into upper case
txt = "Hello my friends" x = txt.upper() print(x)
string.zfill(len)
Fills the string with a specified number of 0 values at the beginning
txt = "50" x = txt.zfill(10) print(x)
728x90'코딩 > Python' 카테고리의 다른 글
[Python tutorial] 22. Dictionary methods (0) 2022.12.21 [Python tutorial] 21. List/Array methods (0) 2022.12.21 [Python tutorial] 19. Built-in Function (0) 2022.12.21 [Python tutorial] 18. File handling (0) 2022.12.21 [Python tutorial] 17. Try...Except (0) 2022.12.21