String methods in Python

In Python, a string is a sequence of characters. Strings are used to represent text, and are often used to store and manipulate data.

1 – str.upper():

This method returns a copy of the string with all uppercase letters. For example:

2 – str.lower():

This method returns a copy of the string with all lowercase letters. For example:

3 – str.capitalize():

This method returns a copy of the string with the first letter capitalized and all other letters in lowercase. For example:

4 – str.strip():

This method returns a copy of the string with leading and trailing whitespace removed. For example:

5 – str.replace(old, new):

This method returns a copy of the string with all occurrences of the old string replaced with the new string. For example:

6 – str.split(separator):

This method returns a list of the words in the string, using the specified separator string as the delimiter. If separator is not specified, the method uses any whitespace as the delimiter. For example:

7 – str.join(iterable):

This method returns a string in which the elements of the iterable are joined with the string on which the method is called. For example:

8 – str.startswith(prefix):

This method returns True if the string starts with the specified prefix, and False otherwise. For example:

9 – str.endswith(suffix):

This method returns True if the string ends with the specified suffix, and False otherwise. For example:

10 – str.find(sub):

This method returns the index of the first occurrence of the sub string within the string, or -1 if the sub string is not found. For example:

11 – str.index(sub):

This method works like str.find(), but raises a ValueError exception if the sub string is not found. For example:

12 – str.isdigit():

This method returns True if the string consists solely of digits, and False otherwise. For example:

13 – str.isalpha():

This method returns True if the string consists solely of alphabetical characters, and False otherwise. For example:

14 – str.isalnum():

This method returns True if the string consists solely of alphabetical characters and digits, and False otherwise. For example:

15 – str.isspace():

This method returns True if the string consists solely of whitespace characters, and False otherwise. For example:

In summary, a string in Python is a sequence of characters that can be manipulated and stored using various methods and operations. Strings are an important data type in Python, and are widely used in many applications.

I hope these examples help you understand some of the common string methods in Python. If you have any questions, please let me know in the comment.

Popular Posts

Author

  • Naveen Pandey Data Scientist Machine Learning Engineer

    Naveen Pandey has more than 2 years of experience in data science and machine learning. He is an experienced Machine Learning Engineer with a strong background in data analysis, natural language processing, and machine learning. Holding a Bachelor of Science in Information Technology from Sikkim Manipal University, he excels in leveraging cutting-edge technologies such as Large Language Models (LLMs), TensorFlow, PyTorch, and Hugging Face to develop innovative solutions.

    View all posts
Spread the knowledge
 
  

Leave a Reply

Your email address will not be published. Required fields are marked *