Python | String Methods

Python | String Methods

Strings are a fundamental data type in Python and are used to represent sequences of characters. Python provides several methods for manipulating and manipulating strings. In this article, we will discuss some of the most commonly used string methods in Python.

1) len(): This is built-in function which returns the length of a string, which is the number of characters in it.
For example:

s = "Hello, World!"
print(len(s)) # 13

2) str.upper(): This method returns a copy of the string in uppercase.
For example:

s = "Hello, World!"
print(s.upper()) # 'HELLO, WORLD!'

3) str.lower(): This method returns a copy of the string in lowercase.
For example:

s = "Hello, World!"
print(s.lower()) # 'hello, world!'

4) str.strip(): This method returns a copy of the string with leading and trailing whitespace characters removed.
For example:

s = "   Hello, World!   "
print(s.strip()) # 'Hello, World!'

5) str.replace(): This method returns a copy of the string with all occurrences of a specified substring replaced with another substring.
For example:

s = "Hello, World!"
print(s.replace("Hello", "Goodbye")) # 'Goodbye, World!'

6) str.split(): This method splits a string into a list of substrings based on a specified separator.
For example:


s = "Hello, World!"
print(s.split(", ")) # ['Hello', 'World!']

7) str.join(): This method concatenates a list of strings into a single string using the string on which it was called as the separator.
For example:


s = ", ".join(["Hello", "World!"])
print(s) # 'Hello, World!'

In conclusion, these are just a few of the most commonly used string methods in Python. By using these methods, we can manipulate and transform strings with ease. Understanding and using these methods is an important part of working with strings in Python.

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds