String Built in Methods
- capitalize() : converts the first character to upper case and rest to lower case.
#program
name = "techno Xpresss"
print(name)
name = name.capitalize()
print(name)
#Output
techno Xpresss
Techno xpresss
- casefold() : Converts string into lower case.
#program
name = "Techno Xpresss"
print(name)
name = name.casefold()
print(name)
#Output
Techno Xpresss
techno xpresss
- center() : Returns a centered string.
- count() : Returns the number of times a specified value occurs in a string.
#program
name = "Techno Xpresss"
print(name)
count_e = name.count('e')
print("count_e presents", count_e, "times")
#Output
Techno Xpresss
count_e presents 2 times
- encode() : Returns an encoded version of the string.
- endswith() : Returns true if the string ends with the specified value.
#program
name = "Techno Xpresss"
print(name)
ends_with_sss = name.endswith('sss')
print(ends_with_sss)
ends_with_no = name.endswith('no')
print(ends_with_no)
#Output
Techno Xpresss
True
False
- expandtabs() : Sets the tab size of the string.
- find() : Searches the string for a specified value and returns the position of where it was found
#program
name = "Techno Xpresss"
print(name)
position = name.find('sss')
print("sss found at", position)
print(name[position : : ])
#Output
Techno Xpresss
sss found at 11
sss
- format() : Formats specified values in a string.
- format_map() : Formats specified values in a string.
- index() : Searches the string for a specified value and returns the position of where it was found.
#program
name = "Techno Xpresss"
print(name)
position_l = name.index('e')
print("found at index : ", position_l)
#Output
Techno Xpresss
found at index : 1
- isalnum() : Returns True if all characters in the string are alphanumeric.
#program
name = "Techno Xpresss"
age = "Twenty8"
print(name)
print(age)
result = name.isalnum()
print(name, "is alphanumeric : ", result)
result = age.isalnum()
print(age, "is alphanumeric : ", result)
#Output
Techno Xpresss
Twenty8
Techno Xpresss is alphanumeric : False
Twenty8 is alphanumeric : True
- isalpha() : Returns True if all characters in the string are in the alphabet.
#program
name = "Techno Xpresss"
age = "TwentyEight"
marks = "Eighty8"
print(name)
print(age)
print(marks)
result = name.isalpha()
print(name, "is alpha : ", result)
result = age.isalpha()
print(age, "is alpha : ", result)
result = marks.isalpha()
print(marks, "is alpha : ", result)
#Output
Techno Xpresss
TwentyEight
Eighty8
Techno Xpresss is alpha : False # space is not an alphabet
TwentyEight is alpha : True
Eighty8 is alpha : False
- isdecimal() : Returns True if all characters in the string are decimals.
#program
name = "Techno Xpresss"
age = "28"
print(name)
print(age)
result = name.isdecimal()
print(name, "have all decimal members : ", result)
result = age.isdecimal()
print(age, "have all decimal members : ", result)
#Output
Techno Xpresss
28
Techno Xpresss have all decimal members : False
28 have all decimal members : True
- isdigit() : Returns True if all characters in the string are digits.
#program
name = "Techno Xpresss"
age = "28"
print(name)
print(age)
result = name.isdigit()
print(name, "have all degits : ", result)
result = age.isdigit()
print(age, "have all digits : ", result)
#Output
Techno Xpresss
28
Techno Xpresss have all degits : False
28 have all digits : True
- isidentifier() : Returns True if the string is an identifier.
- islower() : Returns True if all characters in the string are lower case
#program
name = "Techno Xpresss"
age = "twenty eight"
print(name)
print(age)
result = name.islower()
print(name, "have all lower case : ", result)
result = age.islower()
print(age, "have all lowercase : ", result)
#Output
Techno Xpresss
twenty eight
Techno Xpresss have all lower case : False
twenty eight have all lowercase : True
- isnumeric() : Returns True if all characters in the string are numeric.
#program
name = "Techno Xpresss"
age = "28"
print(name)
print(age)
result = name.isnumeric()
print(name, "have all numeric : ", result)
result = age.isnumeric()
print(age, "have all numeric : ", result)
#Output
Techno Xpresss
28
Techno Xpresss have all numeric : False
28 have all numeric : True
- isprintable() : Returns True if all characters in the string are printable.
#program
name = "Techno Xpresss"
age = "28"
print(name)
print(age)
result = name.isprintable()
print(name, "have all printable members : ", result)
result = age.isprintable()
print(age, "have all printable members : ", result)
#Output
Techno Xpresss
28
Techno Xpresss have all printable members : True
28 have all printable members : True
- isspace() : Returns True if all characters in the string are whitespaces.
#program
name = "Techno Xpresss"
age = " "
print(name)
print(age)
result = name.isspace()
print(name, "have all whitespace : ", result)
result = age.isspace()
print(age, "have all whitespace : ", result)
#Output
Techno Xpresss
Techno Xpresss have all whitespace: False
have all whitespace: True
- istitle() : Returns True if the string follows the rules of a title.
#program
name = "My Name Is Techno Xpresss"
name_1 = "my name is Techno Xpresss"
print(name)
print(name_1)
result = name.istitle()
print(name, "have all whitespace : ", result)
result = name_1.istitle()
print(name_1, "have all whitespace : ", result)
#Output
My Name Is Techno Xpresss
my name is Techno Xpresss
My Name Is Techno Xpresss have all whitespace : True #1st member of each word is upper case
my name is Techno Xpresss have all whitespace : False
- isupper() : Returns True if all characters in the string are upper case.
#program
name = "Techno Xpresss"
name_1 = "TECHNO XPRESSS"
print(name)
print(name_1)
result = name.isupper()
print(name, "have all upper case : ", result)
result = name_1.isupper()
print(name_1, "have all upper case : ", result)
#Output
Techno Xpresss
TECHNO XPRESSS
Techno Xpresss have all upper case : False
TECHNO XPRESSS have all upper case : True
- join() : Joins the elements of an iterable to the end of the string.
#program
name = "-Techno Xpresss "
name_1 = "TECHNO"
print(name)
print(name_1)
result = name.join(name_1)
print(result)
#Output
-Techno Xpresss
TECHNO
T-Techno Xpresss E-Techno Xpresss C-Techno Xpresss H-Techno Xpresss N-Techno Xpresss O
- ljust() : Returns a left justified version of the string.
- lower() : Converts a string into lower case.
#program
name = "Techno Xpresss "
name_1 = "TECHNO"
print(name)
print(name_1)
result = name.lower()
print(result)
result = name_1.lower()
print(result)
#Output
Techno Xpresss
TECHNO
techno xpresss
techno
- lstrip() : Returns a left trim version of the string.
- maketrans() : Returns a translation table to be used in translations.
- partition() : Returns a tuple where the string is parted into three parts.
#program
name = "Techno Xpresss "
name_1 = "TECHNO"
print(name)
print(name_1)
result = name.partition(name)
print(result)
result = name_1.partition(name_1)
print(result)
#Output
Techno Xpresss
TECHNO
('', 'Techno Xpresss ', '')
('', 'TECHNO', '')
- replace() : Returns a string where a specified value is replaced with a specified value.
#program
name = "Techno Xpresss "
print(name)
result = name.replace("sss", "SSS", 3)
print(result)
#Output
Techno Xpresss
Techno XpreSSS
- rfind() : Searches the string for a specified value and returns the last position of where it was found.
#program
name = "Techno Xpresss "
print(name)
position = name.find("e")
print("1st occurance at index :", position)
position = name.rfind("e")
print("last occurance at index :", position)
#Output
Techno Xpresss
1st occurance at index : 1
last occurance at index : 10
- rindex() : Searches the string for a specified value and returns the last position of where it was found.
#program
name = "Techno Xpresss"
print(name)
position_l = name.index('e') # search from left
position_r = name.rindex('e') # search from right
print("found at index : ", position_l)
print("found at index : ", position_r)
#Output
Techno Xpresss
found at index : 1
found at index : 10
- rjust() : Returns a right justified version of the string.
- rpartition() : Returns a tuple where the string is parted into three parts.
- rsplit() : Splits the string at the specified separator, and returns a list.
- rstrip() : Returns a right trim version of the string
- split() : Splits the string at the specified separator, and returns a list.
#program
name = "Techno Xpresss "
print(name)
result = name.split()
print("result :", result)
result = name.split('e')
print("result :", result)
#Output
result : ['Techno', 'Xpresss']
result : ['T', 'chno Xpr', 'sss ']
- splitlines() : Splits the string at line breaks and returns a list.
#program
name = "Techno Xpresss\nage is 28\nmarks scored 80.72% \n"
print(name)
result = name.splitlines()
print("result :", result)
#Output
Techno Xpresss
age is 28
marks scored 80.72%
result : ['Techno Xpresss', 'age is 28', 'marks scored 80.72% ']
- startswith() : Returns true if the string starts with the specified value.
#program
name = "Techno Xpresss"
print(name)
result = name.startswith('T')
print(name, "starts with T :", result)
result = name.startswith('X')
print(name, "starts with X :", result)
#Output
Techno Xpresss
Techno Xpresss starts with T : True
Techno Xpresss starts with X : False
- strip() : Returns a trimmed version of the string.
- swapcase() : Swaps cases, lower case becomes upper case and vice versa.
#program
name = "Techno Xpresss"
print(name)
result = name.swapcase()
print(result)
#Output
Techno Xpresss
tECHNO xPRESSS
- title() : Converts the first character of each word to upper case.
#program
name = "techno xpresss"
print(name)
result = name.title()
print(result)
#Output
techno xpresss
Techno Xpresss
- translate() : Returns a translated string.
- upper() : Converts a string into upper case.
#program
name = "techno xpresss"
print(name)
result = name.upper()
print(result)
#Output
techno xpresss
TECHNO XPRESSS
- zfill() : Fills the string with a specified number of 0 values at the beginning.
Comments
Post a Comment