Various Built-in Functions in Python
Python supports various built in functions to manipulate strings
- len()
- ord()
- chr()
- capitalize()
- center()
- isalnum()
- isalpha()
- isdigit()
- lower()
- islower()
- upper()
- isupper()
- title()
- swapcase()
len()
len() returns the length of characters present in strings. It is used in various fields to calculate the number of characters.
Examples:
#To calculate the no of characters
Blog = "Iterathon"
print(len(Blog))
#Output
9
ord()
ord() returns a the ASCII value of the single character.
Examples:
#To find ASCII Value
print(ord('A'))
#Output
65
chr()
chr() returns a characters are equally represented by ASCII values.
Examples:
#To find a characters equal to ASCII values
print(chr(65))
#Output
A
To know more about ASCII (American Standard Code for Information Interchange). Visit this site: https://www.ascii-code.com/ (not promotion, just for useful knowledge)
captalize()
capitalize() is used to capitalize a first letter of the string.
Examples:
#capitalize first letter in string
blog = "iterathon"
print(blog.capitalize())
#Output
Iterathon
center()
center() is used to center the given word with fill character.
syntax: center(width, fill character)
Example:
#To center the string with specified character Blog = "Iterathon" print(Blog.center(15,'*') #Output ***Iterathon***
isalnum()
if the string contains only alphabets and numbers it returns True, otherwise False.
Example 1:
Blog = "Iterathon"
print(Blog.isalnum())
#Output
True
Example 2:
Blog = "Iterathon-Blog"
print(Blog.isalnum())
#Output
False
isalpha()
If the string contains only letters it returns True, otherwise return False.
Example:
Blog = "Iterathon"
print(Blog.isalpha())
#Output
True
isdigit()
If the string contains only numbers it returns True, Otherwise it returns False.
Example:
Blog = "12345"
print(Blog.isdigit())
#Output
True
lower()
lower() is used to convert all characters in a string to lowercase.
Example:
Blog = "ITERATHON"
print(Blog.lower())
#Output
iterathon
islower()
If the string is in lowercase returns True otherwise False.
Example:
Blog = "Iterathon"
print(Blog.islower())
#Output
False
upper()
upper() is used to convert all characters in a string to uppercase.
Example:
Blog = "Iterathon"
print(Blog.upper())
#Output
ITERATHON
isupper()
If the string is in uppercase returns True otherwise False.
Example:
Blog = "Iterathon"
print(Blog.isupper())
#Output
False
title()
title() function returns a string in title format.
Example:
Blog = "iterathon"
print(Blog.title())
#Output
Iterathon
swapcase()
It converts all uppercase to lowercase and all lowercase to uppercase.
Example:
Blog = "ITERAthon"
print(Blog.swapcase())
#Output
iteraTHON
For More Knowledge
Important List Operations in Python: https://iterathon.tech//important-list-operations-in-python/
LEARN LITTLE BY LITTLE TO CHANGE THE TECH WORLD