String Methods in Python | CBSE – Class 12
In this section, “Python String Methods: A Beginner’s Guide”, we will provide a comprehensive introduction to python String method, including
- What are String Methods in python?.
- Commonly Used String Methods.
- Examples of String Methods in Python.
- Tips and Tricks for Working with Python Strings.
- Built-in methods that you can use on strings.
And, by the end of this tutorial, readers will have a solid understanding of A number of built-in methods of string in Python and will be able to use this knowledge in their own programming projects.
Also this tutorial covers all necessary topics/concepts required to complete your exams preparations in CBSE schools / classes.
Introduction:
Strings are one of the most important data types in Python. They are used to store textual data and can be manipulated in various ways using string methods.
In this beginner’s guide, we’ll explore some of the most commonly used Python string methods and how to use them.
What are String Methods?
Python string methods are built-in functions that can be used to manipulate strings. They provide an easy and efficient way to perform common string operations, such as changing the case of a string, replacing characters, or searching for specific substrings.
List of built-In string methods:
Built-in methods that you can use on strings.
S.N | METHOD | DESCRIPTION |
---|---|---|
1 | capitalize(): | Converts the first character to upper case |
2 | casefold() | Converts string into lower case |
3 | center() | Returns a centered string |
4 | count() | Returns the number of times a specified value occurs in a string |
5 | encode() | Returns an encoded version of the string |
6 | endswith() | Returns true if the string ends with the specified value |
7 | expandtabs() | Sets the tab size of the string |
8 | join() | Converts the elements of an iterable into a string |
9 | ljust() | Returns a left justified version of the string |
10 | lower() | Converts a string into lower case |
11 | lstrip() | Returns a left trim version of the string |
12 | maketrans() | Returns a translation table to be used in translations |
13 | partition() | Returns a tuple where the string is parted into three parts |
14 | replace() | Returns a string where a specified value is replaced with a specified value |
15 | rfind() | Searches the string for a specified value and returns the last position of where it was found |
16 | rindex() | Searches the string for a specified value and returns the last position of where it was found |
17 | rjust() | Returns a right justified version of the string |
18 | rpartition() | Returns a tuple where the string is parted into three parts |
19 | rsplit() | Splits the string at the specified separator, and returns a list |
20 | rstrip() | Returns a right trim version of the string |
21 | split() | Splits the string at the specified separator, and returns a list |
22 | splitlines() | Splits the string at line breaks and returns a list |
23 | startswith() | Returns true if the string starts with the specified value |
24 | strip() | Returns a trimmed version of the string |
25 | swapcase() | Swaps cases, lower case becomes upper case and vice versa |
26 | title() | Converts the first character of each word to upper case |
27 | translate() | Returns a translated string |
28 | isalnum() | Returns True if all characters in the string are alphanumeric |
29 | isalpha() | Returns True if all characters in the string are in the alphabet |
30 | isascii() | Returns True if all characters in the string are ascii characters |
31 | isdecimal() | Returns True if all characters in the string are decimals |
32 | isdigit() | Returns True if all characters in the string are digits |
33 | isidentifier() | Returns True if the string is an identifier |
34 | islower() | Returns True if all characters in the string are lower case |
35 | isnumeric() | Returns True if all characters in the string are numeric |
36 | isprintable() | Returns True if all characters in the string are printable |
37 | isupper() | Returns True if all characters in the string are upper case |
38 | istitle() | Returns True if the string follows the rules of a title |
39 | isspace() | Returns True if all characters in the string are whitespaces |
40 | index() | Searches the string for a specified value and returns the position of where it was found |
41 | format_map() | Formats specified values in a string |
42 | format() | Formats specified values in a string |
43 | find() | Searches the string for a specified value and returns the position of where it was found |
44 | upper() | Converts a string into upper case |
45 | zfill() | Fills the string with a specified number of 0 values at the beginning |
Commonly Used String Methods:
There are many Python string methods available, but here are some of the most commonly used:
.lower()
: Converts all the characters in a string to lowercase..upper()
: This method converts all the characters in a string to uppercase..replace()
: Replaces a substring with another substring in a string..split()
: Method splits a string into a list of substrings based on a delimiter..startswith()
: Method checks if a string starts with a given substring..endswith()
: Checks if a string ends with a given substring..strip()
: This method removes leading and trailing whitespace characters from a string.
Examples of String Methods:
Let’s take a look at some examples of how to use these methods or built-n functions.
Example 1: Using .lower()
and .upper()
my_string = "Hello World"
print(my_string.lower())
print(my_string.upper())
Output:
hello world
HELLO WORLD
Example 2: Using .replace()
my_string = "Python is fun"
print(my_string.replace("fun", "awesome"))
Output:
Python is awesome
Example 3: Using .split()
my_string = "apple, banana, cherry"
print(my_string.split(", "))
Output:
[‘apple’, ‘banana’, ‘cherry’]
Tips and Tricks for Working with Python Strings:
Here are some tips and tricks to help you work more efficiently with Python strings:
- Use string formatting to insert variables into a string.
- and use string slicing to extract substrings from a string.
- Also use regular expressions for more complex string operations.
Conclusion:
In this beginner’s guide, we’ve covered some of the most commonly used string methods and how to use them. By mastering these methods, you’ll be able to manipulate strings in various ways and perform common string operations.
Remember to practice and experiment with these methods to become proficient in using them. Happy coding!