A string is a series of Unicode characters surrounded by quotation marks. A character is nothing more than a symbol. Computers do not work with characters; instead, they work with numbers (binary). Even though you see characters on your screen, they are saved and modified inside as a series of 0s and 1s. The most common conversion mechanisms used by programming languages are ASCII and Unicode. This article focuses on various Python string methods that are being used for string handling and manipulation.
Creating Python Strings
Strings are formed by enclosing characters in single or double quotations. In Python, triple quotes can also be used to denote multiline texts and docstrings.
Example
# Python program to create a string
# using single-quotation
txt = 'Hello World!'
print('Created string:', txt)
# using double-quotation
txt = "Good Morning Students"
print('Created string:', txt)
# using triple-quotes
txt = '''I love Python Programming'''
print('Created string:', txt)
Output
Created string: Hello World!
Created string: Good Morning Students
Created string: I love Python Programming
Python String Methods
Python, like the set, list, and dictionary class methods, has a plethora of built-in functions for manipulating strings. Python String Methods are a set of built-in methods that you can use on strings for data handling and manipulation. Because Python Strings are immutable, all of these functions return a new string while leaving the previous string untouched and unchanged.
Method |
Description |
Python String capitalize() | Returns a string with the first character converted to Uppercase while other letters to lowercase |
Python String casefold() | Returns a string converting a wider range of characters to lowercase |
Python String center() | Pads the input string to the center with the specified character |
Python String count() | Returns the number of occurrences of a substring from the original string |
Python String encode() | Returns an encoded string with the specified encoding standard |
Python String endswith() | Returns value TRUE if the string ends with the specified suffix |
Python String expandtabs() | Replaces tab characters \t with spaces |
Python String find() | Returns the index value of the first occurrence of the substring |
Python String format() | Formats the string by converting it into a more representative output format |
Python String format_map() | Formats the string values using a dictionary |
Python String index() | Returns the index value of the substring specified |
Python String isalnum() | Checks if all characters are alphanumeric |
Python String isalpha() | Checks if all characters are alphabets |
Python String isdecimal() | Checks if all characters are decimal values |
Python String isdigit() | Checks if all characters are digits or not |
Python String isidentifier() | Checks if all characters are valid identifiers |
Python String islower() | Checks if all characters in a string are lowercased |
Python String isnumeric() | Checks if all characters are numeric |
Python String isprintable() | Checks if all characters are printable |
Python String isspace() | Checks if all characters contain whitespaces |
Python String istitle() | Returns TRUE if the string is a title cased string |
Python String issuper() | Returns TRUE if all the characters in the string are uppercase |
Python String join() | Returns a concatenated string |
Python String ljust() | Returns a left-justified string as per the width specified |
Python String lower() | Converts all the characters of the string to lowercase |
Python String lstrip() | Returns a string with the leading characters removed |
Python String maketrans() | Create a mapping table and returns this translation table |
Python String partition() | Splits the string at the first occurrence of the separator |
Python String replace() | Replaces all the occurrences of the specified substring with another substring |
Python String rfind() | Returns the highest index value of the substring |
Python String rindex() | Returns the highest index value of the substring inside the string |
Python String rjust() | Returns a right-justified string as per the width specified |
Python String rpartition() | Splits the string into 3 different parts and returns a tuple |
Python String rsplit() | Splits the string from right |
Python String rstrip() | Returns a string with all the trailing characters removed |
Python String split() | Splits the string from the specified separator and returns a list |
Python String splitlines() | Splits the string at line boundaries |
Python String startswith() | Returns value TRUE if the string starts with the specified prefix |
Python String strip() | Removes both the leading and trailing characters of a string |
Python String swapcase() | Swaps uppercase characters to lowercase and vice versa |
Python String title() | Converts the string to a tile cased string |
Python String translate() | Modifies the string according to the translation table |
Python String upper() | Converts all the characters of the string to uppercase |
Python String zfill() | Returns a copy of the string with ‘0’ padded to the left of the string |
Frequently Asked Questions
Q1. What are Python string methods?
A string is a series of Unicode characters surrounded by quotation marks. In Python, strings can be used to manage textual data. Strings in Python are immutable sequences of Unicode points. To perform certain operations and functionalities on strings, Python provides us with a wide range of string manipulation methods.
Python, like the set, list, and dictionary class methods, has a plethora of built-in functions for manipulating strings. Because Python Strings are immutable, all of these functions return a new string while leaving the previous string untouched and unchanged.
Q2. Does == work for strings in Python?
When working with strings, you may wish to check to determine if one string is equivalent to another.  Python provides us with various string comparison operators, but one of the most commonly used is the ‘==’ equality operator. If the strings are equal, Boolean value TRUE is returned, else FALSE.
Example
str1 = 'Hello'
str2 = 'hello'
print('Are the strings equal:', str1 == str2)
str1 = 'Python'
str2 = 'Python'
print('Are the strings equal:', str1 == str2)
Output
Are the strings equal: False
Are the strings equal: True
Leave a Reply