Python - String Methods
Python provides a wide range of built-in methods that you can use to manipulate and work with strings. These methods allow you to perform various operations on strings, such as converting case, searching for substrings, and more. It's important to note that all string methods return new values and do not modify the original string.
Here's a list of commonly used string methods in Python:
capitalize()
Converts the first character of a string to uppercase.
casefold()
Converts the string to lowercase.
center()
Returns a centered string within a specified width.
count()
Returns the number of occurrences of a substring.
encode()
Returns an encoded version of the string.
endswith()
Checks if the string ends with a specified value.
expandtabs()
Sets the tab size of the string.
find()
Searches for a substring and returns its position.
format()
Formats a string by replacing placeholders with values.
format_map()
Similar to format()
, but uses a mapping to replace values.
index()
Searches for a substring and returns its position.
isalnum()
Returns True
if all characters are alphanumeric.
isalpha()
Returns True
if all characters are alphabetic.
isascii()
Returns True
if all characters are ASCII characters.
isdecimal()
Returns True
if all characters are decimals.
isdigit()
Returns True
if all characters are digits.
isidentifier()
Returns True
if the string is a valid identifier.
islower()
Returns True
if all characters are lowercase.
isnumeric()
Returns True
if all characters are numeric.
isprintable()
Returns True
if all characters are printable.
isspace()
Returns True
if all characters are whitespace.
istitle()
Returns True
if the string follows title case rules.
isupper()
Returns True
if all characters are uppercase.
join()
Joins elements of an iterable to the end of the string.
ljust()
Returns a left-justified version of the string.
lower()
Converts the string to lowercase.
lstrip()
Removes leading whitespace from the string.
maketrans()
Returns a translation table for use in translate()
.
partition()
Splits the string into three parts based on a separator.
replace()
Replaces a specified value with another value.
rfind()
Searches for a substring and returns the last position.
rindex()
Searches for a substring and returns the last position.
rjust()
Returns a right-justified version of the string.
rpartition()
Splits the string into three parts from the right.
rsplit()
Splits the string at a specified separator from the right.
rstrip()
Removes trailing whitespace from the string.
split()
Splits the string at a specified separator and returns a list.
splitlines()
Splits the string at line breaks and returns a list.
startswith()
Checks if the string starts with a specified value.
strip()
Removes leading and trailing whitespace.
swapcase()
Swaps the case of characters (lower to upper and vice versa).
title()
Converts the first character of each word to uppercase.
translate()
Returns a translated string based on a translation table.
upper()
Converts the string to uppercase.
zfill()
Fills the string with zeros up to a specified width.
These methods provide powerful tools for working with strings and can be used in various scenarios to process and manipulate text data.
Last updated