Session – 10(Strings 2-3)

  • String Built-in Methods (1/2)

String Built-in Methods (1/2)

Python provides a lot of built-in functions to manipulate strings. Python String is immutable, so all these functions return a new string and the original string remains unchanged.

FunctionDescription
format()It’s used to create a formatted string from the template string and the supplied values.
split()Python string split() function is used to split a string into the list of strings based on a delimiter.
join()This function returns a new string that is the concatenation of the strings in iterable with string object as a delimiter.
strip()Used to trim whitespaces from the string object.
format_map()Python string format_map() function returns a formatted version of the string using substitutions from the mapping provided.
upper()We can convert a string to uppercase in Python using str.upper() function.
lower()This function creates a new string in lowercase.
replace()Python string replace() function is used to create a new string by replacing some parts of another string.
find()Python String find() method is used to find the index of a substring in a string.
translate()Python String translate() function returns a new string with each character in the string replaced using the given translation table.
FunctionDescription
encode()Python string encode() function is used to encode the string using the provided encoding.
count()Python String count() function returns the number of occurrences of a substring in the given string.
startswith()Python string startswith() function returns True if the string starts with the given prefix, otherwise it returns False.
endswith()Python string endswith() function returns True if the string ends with the given suffix, otherwise it returns False.
capitalize()Python String capitalize() function returns the capitalized version of the string.
center()Python string center() function returns a centered string of specified size.
casefold()Python string casefold() function returns a casefolded copy of the string. This function is used to perform case-insensitive string comparison.
expandtabs()Python string expandtabs() function returns a new string with tab characters (\t) replaced with one or more whitespaces.
index()Python String index() function returns the lowest index where the specified substring is found.
__contains__()Python String class has __contains__() function that we can use to check if it contains another string or not. We can also use “in” operator to perform this check.

Leave a comment