Home > Backend Development > Python Tutorial > String Functions

String Functions

Susan Sarandon
Release: 2024-11-19 18:48:03
Original
971 people have browsed it

String Functions

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()) 
Copy after login
Pritha
Copy after login

2.casefold():Converts string into lower case

name = "PRITHA"
print(name.casefold()) 
Copy after login
pritha
Copy after login

3.center():Returns a centered string

name = "pritha"
print(name.center(10,"-")) 
Copy after login
--pritha--
Copy after login

4.count():Returns the number of times a specified value occurs in a string

name = "lakshmipritha"
print(name.count('a')) 
Copy after login
2
Copy after login

5.encode():Returns an encoded version of the string

name = "lakshmipritha"
print(name.encode()) 
Copy after login
b'lakshmipritha'
Copy after login

6.endswith():Returns true if the string ends with the specified value

name = "lakshmi pritha"
print(name.endswith('pritha')) 
Copy after login
True
Copy after login

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')) 
Copy after login
8
Copy after login
Copy after login

8.format():Formats specified values in a string

name = "Hello, {}. Welcome to {}."
print(name.format("Pritha", "Python")) 
Copy after login
Hello, Pritha. Welcome to Python.
Copy after login

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))

Copy after login
My name is Pritha and I am 30 years old.
Copy after login

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)
Copy after login
8
Copy after login
Copy after login

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())

Copy after login
True
True
True
False
False
True
True
False
True
False
False
False





Copy after login

The above is the detailed content of String Functions. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template