Skip to main content

String Functions in Python

Enter your email address:

मैल पर फीड बर्नर से प्राप्त लिंक Verify करे


RUN YOUR PYTHON CODE ONLINE  HERE(COPY AND PASTE CODE HERE)
String inbuilt functions 

capitalize()
Converts the first character to upper case

s1="meraprays"
s2=s1.capitalize()
print(s2)
Output: 
Meraprayas
lower()
Converts a string into lower case



swapcase()
Swaps cases, lower case becomes upper case and vice versa

s1="MERAprayas"
s2=s1.swapcase()
print(s2)
Output: meraPRAYAS

title()
Converts the first character of each word to upper case.
s1="hello om shanti "
s2=s1.title()
print(s2)
Output: 
Hello Om Shanti 

upper()
Converts a string into upper case
s1="hello om shanti "
s2=s1.upper()
print(s2)
Output: 
Hello Om Shanti 


isalnum()        
Returns True if all characters in the string are alphanumeric
s1="MERAPRAYAS123"
s2=s1.isalnum()
print(s2)
Output: true

isalpha()  
Return True if all character in the string are alphabet
s1="MERAPRAYAS"
s2=s1.isalpha()
print(s2)
Output: true


isdigit()
Returns True if all characters in the string are digits
s1="MERAPRAYAS"
d=s1.isdigit()
print(d)
Output: false


isidentifier()
Returns True if the string is an identifier
s1="MERAPRAYAS"
i=s1.isidentifier()
print(i)
Output: true


islower()
Returns True if all characters in the string are lower case
s1="MERAPRAYAS"
l=s1.islower()
print(l)
Output: false

isnumeric()
Returns True if all characters in the string are numeric

s1="123454"
n=s1.isnumeric()
print(n)
Output: true


isprintable()
Returns True if all characters in the string are printable
s1="123454"
p=s1.isprintable()
print(p)
Output: true

isspace()
Returns True if all characters in the string are whitespaces
s1="      "
x=s1.isspace()
print(x)
Output: true

istitle()
Returns True if the string follows the rules of a title
s1="Meraprayas "  # first character capital
x=s1.istitle()
print(x)
Output: true

isupper()
Returns True if all characters in the string are upper case
s1="MERAPRAYAS "
x=s1.isupper()
print(x)
Output: true

endswith()
Returns true if the string ends with the specified value
s1="MERA PRAYAS  MERA PRAYAS  MERA "
n=s1.endswith("RA")
print(n)
Output: true
startswith()
Returns true if the string starts with the specified value
s1="MERA PRAYAS  MERA PRAYAS  MERA "
n=s1.startswith("ME")
print(n)
Output: true




count()
Returns the number of times a specified value occurs in a string.
It is case sensitive.

s1="MERA PRAYAS  MERA PRAYAS  MERA "
n=s1.count("MERA")
print(n)
Output: 3

find()
Searches the string for a specified value and returns the position of where it was found

s1="MERA PRAYAS  MERA PRAYAS  MERA "
n=s1.find("PRAYAS")
print(n)
Output: 5


rfind()
Searches the string for a specified value and returns the last position of where it was   found

s1="MERA PRAYAS  MERA PRAYAS  MERA "
n=s1.rfind("PRAYAS")
print(n)
Output: 18

expandtabs()
Sets the tab size of the string.

s1="MERA \t PRAYAS \t MERA \t PRAYAS\t  MERA "
s2=s1.expandtabs("5")
print(s2)
Output: 
MERA       PRAYAS    MERA      PRAYAS     MERA 


format()   
Formats specified values in a string

s1 = "Only {price:.2f} Rupees"
print(s1.format(price = 149))

s2 = "Only {price:.3f} Rupees"
print(s2.format(price = 149))

output: 
Only 149.00 Rupees
Only 149.000 Rupees


index()
Searches the string for a specified value and returns the position of where it was found

S = "OM SHANTI JI HELLO OM SHANTI"

x = S.index("SHANTI")

print(x)

output: 3

rindex()
Searches the string for a specified value and returns the last position of where it was  found

S = "OM SHANTI OM SHANTI"

x = S.rindex("SHANTI")

print(x)

output: 13

join()
Joins the elements of an iterable to the end of the string

T = ("ARAWALI", "UDAIGIRI", "SHIVAIK")

x = "->".join(myTuple)

print(x)

ouput
ARAWALI->UDAIGIRI->SHIVAIK

rjust()
Returns a right justified version of the string

txt = "banana"
x = txt.rjust(20)
print(x, "is my favorite fruit.")
ouput:
                      banana is my favorite fruit.




ljust()
Returns a left justified version of the string

txt = "banana"
x = txt.ljust(20)
print(x, "is my favorite fruit.")

ouput:
banana               is my favorite fruit.

center()
Returns a center justified version of the string

txt = "banana"
x = txt.center(20)
print(x, "is my favorite fruit.")

ouput:
          banana            is my favorite fruit.





partition()
Returns a tuple where the string is parted into three parts

s = " I want to eat samosa.  samosa was very testy"
x = s.partition("eat")
print(x)
ouput
(' I wamt to ', 'eat', ' samosa')

rpartition()
Returns a tuple where the string is parted into three parts.

The rpartition() method searches for the last occurrence of a specified string, and splits the string into a tuple containing three elements.
The first element contains the part before the specified string.
The second element contains the specified string.
The third element contains the part after the string.


s = " I want to eat samosa.samosa to eat samosa was very test"

x = s.rpartition("eat")

print(x)

output

(' I want to eat samosa.samosa to ', 'eat', ' samosa was very test')

replace()
Returns a string where a specified value is replaced with a specified value
s = "I want to eat samosa"

x = s.replace("samosa", "apple")

print(x)
output
I want to eat apple


split()
Splits the string at the specified separator, and returns a list
s = " I want to eat samosa.It was very test"

x = s.split()

print(x)

output:
['I', 'want', 'to', 'eat', 'samosa.It', 'was', 'very', 'test']




rsplit()
Splits the string at the specified separator, and returns a list
s = " I want to eat samosa.It was very test"

x = s.rsplit(".")

print(x)
output
[' I want to eat samosa', 'It was very test']


splitlines()
Splits the string at line breaks and returns a list
s = "I want to eat apple \n I want to eat samosa"

x = s.splitlines()

print(x)
ouput:
['I want to eat apple ', ' I want to eat samosa']

strip()
Returns a trimmed version of the string
txt = "     banana     "
x = txt.rstrip()
print("of all fruits", x, "is my favorite")

ouput
of all fruits banana  is my favorite

rstrip()

Returns a right trim version of the string
txt = "     banana     "
x = txt.rstrip()
print("of all fruits", x, "is my favorite")
output
of all fruits      banana  is my favorite

lstrip()
Returns a left trim(remove space from left) version of the string
txt = "     banana     "
x = txt.lstrip()
print("of all fruits", x, "is my favorite")

output
of all fruits banana      is my favorite


zfill()
Fills the string with a specified number of 0 values at the beginning

s = "50"
x = s.zfill(10)
print(x)
output
0000000050

casefold()
Converts string into lower case

s1="MERAPRAYAS"
s2=s1.casefold()
print(s2)
Output: meraprays



Comments

Popular posts from this blog

Revision basics of Python covered in Class XI

Writing CSV Files