String Methods

  • In Python, string methods are built-in functions that can be applied tostring objects to perform various operations and manipulations on strings.
  • Here’s an overview of some commonly used string methods in Python.

1. 'upper()' and 'lower()' :

  • ‘upper()’ : Converts all characters in a string to uppercase.
  • ‘lower()’ : Converts all characters in a string to lowercase.

2. 'capitalize()' :

  • Converts the first character of a string to uppercase and all other characters to lowercase.

3. 'count(substring)' :

  • Returns the number of occurrences of a substring within a string.

4. 'startswith(prefix)' and 'endswith(suffix)' :

  • startswith(prefix): Checks if a string starts with a specified prefix.
  • endswith(suffix): Checks if a string ends with a specified suffix.

5. 'replace(old, new)' :

  • Replaces all occurrences of a specified substring with a new substring.

6. 'split(delimiter,maxsplit)' :

  • Splits a string into a list of substrings based on a specified delimiter.
  • Delimiter is the separator to divide the string into multiple part. By default any whitespace is a delimiter.
  • Maxsplit specifies how many splits are to be done. By default it’s value is -1.

7. 'strip()', 'lstrip()', and 'rstrip()' :

  • strip() : Removes leading and trailing whitespace characters from a string.
  • lstrip() : Removes leading whitespace characters from a string in the left.
  • rstrip() : Removes leading whitespace characters from a string in the right.

8. 'islower()' :

  • Returns True if all characters in the string are lower case.

9. 'isdigit()' :

  • Returns True if all characters in the string are digits.

10. 'find()' :

  • This method in python searches the string for a specified value and returns the position of where it was found.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x