Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 29, 2021 10:34 am GMT

What are basic string methods in Python?

In Python, strings are immutable.This means that they can not be modified.So if we wanted to repair a typo in string,we cant simply modify the incorrect character.
We would need to create a replacement string with typo corrected.We can also assign a replacement value to the variable holding our string.
If we arent sure what the index of our type is, we will use the string method index to locate it and return the index. lets say weve the stringlions tigers and bears within the variable animals.We can locate the index that contains the letter g using animals index(9), which can return the index; during this case 8.
We can also use sub strings to locate the index where the sub string begins, animal index(bears) would return 17, since that is the start of the sub string.If theres quite one match for a sub string, the index method will return the primary match.If we attempt to locate a sub string that does not exist within the string, well receive a worth Error explaining that the sub string wasnt found.
We can avoid a worth Error by first checking if the sub string exists within the string.This can be done using the in keyword.
In this case, it is a conditional which will be either True or False.If the sub string is found within the string, itll be True, if the sub string isnt found within the string, itll be False.
Using our previous variable animals, we will do horses in animals to see if the sub string horses is found in our variable.In this case, it might evaluate to False,since horses arent included in our example string.If we did tigers in animals, wed get True, since this sub string is contained in our string.
Method Description

capitalize() Converts the primary character to capital
casefold() Converts string into small letter
center() Returns a centered string
count() Returns the amount of times a specified value occurs during a string
encode() Returns an encoded version of the string
endswith() Returns true if the string ends with the required value
expandtabs() Sets the tab size of the string
find() Searches the string for a specified value and returns the position of where it had been found
format() Formats specified values during a string
format_map() Formats specified values during a string
index() Searches the string for a specified value and returns the position of where it had been found
isalnum() Returns True if all characters within the string are alphanumeric
isalpha() Returns True if all characters within the string are within the alphabet
isdecimal() Returns True if all characters within the string are decimals
isdigit() Returns True if all characters within the string are digits
isidentifier() Returns True if the string is an identifier
islower() Returns True if all characters within the string are small letter
isnumeric() Returns True if all characters within the string are numeric
isprintable() Returns True if all characters within the string are printable
isspace() Returns True if all characters within the string are whitespaces
istitle() Returns True if the string follows the principles of a title
isupper() Returns True if all characters within the string are capital
join() Joins the weather of an iterable to the top of the string
ljust() Returns a left justified version of the string
lower() Converts a string into small letter
lstrip() Returns a left trim version of the string
maketrans() Returns a translation table to be utilized in translations
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of where it had been found
rindex() Searches the string for a specified value and returns the last position of where it had been found
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 required separator, and returns an inventory
rstrip() Returns a right trim version of the string
split() Splits the string at the required separator, and returns an inventory
splitlines() Splits the string at line breaks and returns an inventory
startswith() Returns true if the string starts with the required value
strip() Returns a trimmed version of the string
swapcase() Swaps cases, small letter becomes capital and the other way around
title() Converts the primary character of every word to capital
translate() Returns a translated string
upper() Converts a string into capital
zfill() Fills the string with a specified number of 0 values at the start

For more details visit:https://www.technologiesinindustry4.com/2020/09/what-are-basic-string-methods-in-python.html


Original Link: https://dev.to/ahmedmansoor012/what-are-basic-string-methods-in-python-283a

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To