[go: up one dir, main page]

DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

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()) 
Enter fullscreen mode Exit fullscreen mode
Pritha
Enter fullscreen mode Exit fullscreen mode

2.casefold():Converts string into lower case

name = "PRITHA"
print(name.casefold()) 
Enter fullscreen mode Exit fullscreen mode
pritha
Enter fullscreen mode Exit fullscreen mode

3.center():Returns a centered string

name = "pritha"
print(name.center(10,"-")) 
Enter fullscreen mode Exit fullscreen mode
--pritha--
Enter fullscreen mode Exit fullscreen mode

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

name = "lakshmipritha"
print(name.count('a')) 
Enter fullscreen mode Exit fullscreen mode
2
Enter fullscreen mode Exit fullscreen mode

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

name = "lakshmipritha"
print(name.encode()) 
Enter fullscreen mode Exit fullscreen mode
b'lakshmipritha'
Enter fullscreen mode Exit fullscreen mode

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

name = "lakshmi pritha"
print(name.endswith('pritha')) 
Enter fullscreen mode Exit fullscreen mode
True
Enter fullscreen mode Exit fullscreen mode

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')) 
Enter fullscreen mode Exit fullscreen mode
8
Enter fullscreen mode Exit fullscreen mode

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

name = "Hello, {}. Welcome to {}."
print(name.format("Pritha", "Python")) 
Enter fullscreen mode Exit fullscreen mode
Hello, Pritha. Welcome to Python.
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode
My name is Pritha and I am 30 years old.
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
8
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode
True
True
True
False
False
True
True
False
True
False
False
False





Enter fullscreen mode Exit fullscreen mode

Top comments (0)