String functions in Python are used to modify information with a string or query. Python has a string data type that has several string functions that handle strings directly.
We’ll be covering the following topics in this tutorial:
Built-in String Methods
In this tutorial, you can learn Python string functions. Python has many built-in functions to manipulate strings and character values.
Note: Python String is immutable, so string methods return new values. They do not change the original string.
This section describes built-in method for converting a string of characters to its applicable new string.
Python String capitalize()
returns a string copy with only its first character capitalized.
Python String center()
aligns the string center with the string’s left and right padding
Python String count()
returns all the old substring occurrences with the new substring
Python String encode()
returns encoded string of given string
Python String endswith()
returns True if the string ends with the specified suffix
Python String expandtabs()
returns a copy of the string in which tab characters ie. ‘\t’ are expanded using spaces
Python String find()
returns the first matched index of the substring
Python String index()
Returns Index of Substring
Python String isalnum()
tests whether or not all characters are alphanumeric
Python String isalpha()
returns true if all the string characters are alphabets
Python String isdecimal()
Checks all the characters in the string are decimal
Python String isdigit()
returns True if all the characters are digits
Python String islower()
returns True if all string characters are in lowercase
Python String isnumeric()
checks whether the string consists of only numeric characters
Python String isspace()
returns true if there are only whitespace characters in the string
Python String istitle()
tests and returns True if the only first character of each word is uppercase
Python String isupper()
checks whether all the case-based characters (letters) of the string are uppercase
Python String join()
used to join separator items to the input string
Python String ljust()
returns a left-justified string and fills the remaining spaces with specified fillchar
Python String lower()
returns the lowercased string from the given string
Python String lstrip()
returns a copy of the string in which all chars have been stripped from the beginning of the string
Python String maketrans()
returns a mapping table that maps every character in the intabstring in the same place in the outtab string
Python String replace()
returns all the old substring occurrences with the new substring
Python String rfind()
returns the index of the string’s rightest matched substring
Python String rindex()
returns the last index of the substring inside the string
Python String rjust()
returns the right-justified string and fill the remaining spaces with specified fillchar
Python String rstrip()
returns a copy of the string in which all chars have been stripped from the end of the string
Python String split()
returns a comma-separated list, using separator delimiter
Python String splitlines()
returns a list with all the lines in string, optionally including the line breaks.
Python String startswith()
returns True if the string starts with the specified prefix, otherwise False.
Python String strip()
returns a copy of the string by stripping both the beginning and the string’s end
Python String swapcase()
transforms the case of uppercase string characters to lowercase and vice versa
Python String title()
returns a copy of the string in which first characters of all the words are capitalized
Python String translate()
returns a copy of the string in which a specified translation table is used to map each character
Python String upper()
transforms all lowercase characters into uppercase and returns an uppercase string
Python String zfill()
returns the copy of the string with 0 on the left