Python string functions:
Python has a set of built-in methods that you can use on strings.
All string methods returns new values. They do not change the original string.
1.**capitalize(): **Capitalizes the first character of the string.
name = "pritha" print(name.capitalize())
Pritha
2.casefold():Converts string into lower case
name = "PRITHA" print(name.casefold())
pritha
3.center():Returns a centered string
name = "pritha" print(name.center(10,"-"))
--pritha--
4.count():Returns the number of times a specified value occurs in a string
name = "lakshmipritha" print(name.count('a'))
2
5.encode():Returns an encoded version of the string
name = "lakshmipritha" print(name.encode())
b'lakshmipritha'
6.endswith():Returns true if the string ends with the specified value
name = "lakshmi pritha" print(name.endswith('pritha'))
True
7.find():Searches the string for a specified value and returns the position of where it was found
name = "lakshmi pritha" print(name.find('pritha'))
8
8.format():Formats specified values in a string
name = "Hello, {}. Welcome to {}." print(name.format("Pritha", "Python"))
Hello, Pritha. Welcome to Python.
9.format_map():Formats specified values in a string
text = "My name is {name} and I am {age} years old." data = {"name": "Pritha", "age":30 } print(text.format_map(data))
My name is Pritha and I am 30 years old.
10.index():Searches the string for a specified value and returns the position of where it was found
name= "lakshmi pritha" position = name.index("pritha") print(position)
8
11.isalnum():Returns True if all characters in the string are alphanumeric
12.isalpha():Returns True if all characters in the string are in the alphabet
13.isascii():Returns True if all characters in the string are ascii characters
14.isdecimal():Returns True if all characters in the string are decimals
15.isdigit():Returns True if all characters in the string are digits
16.isidentifier():Returns True if the string is an identifier
17.islower():Returns True if all characters in the string are lower case
18.isnumeric():Returns True if all characters in the string are numeric
19.isprintable():Returns True if all characters in the string are printable
20.isspace():Returns True if all characters in the string are whitespaces
21.istitle():Returns True if the string follows the rules of a title
22.isupper():Returns True if all characters in the string are upper case
name = "pritha" print(name.isalnum()) print(name.isalpha()) print(name.isascii()) print(name.isdecimal()) print(name.isdigit()) print(name.isidentifier()) print(name.islower()) print(name.isnumeric()) print(name.isprintable()) print(name.isspace()) print(name.istitle()) print(name.isupper())
True True True False False True True False True False False False
The above is the detailed content of String Functions. For more information, please follow other related articles on the PHP Chinese website!