Your ultimate guide

String Functions in Python

String Functions in Python:
    Python has a variety of built-in string methods. The number of string methods in Python can vary depending on the version of Python you are using, but here are some commonly used string methods in Python:
  1. str.upper(): Converts a string to uppercase.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> print(text.upper())
    • >>> output: TESTING COLLEGES
  2. str.lower(): Converts a string to lowercase.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> print(text.lower())
    • >>> output: testing colleges
  3. str.capitalize(): Capitalizes the first letter of the string.
    • Example:
    • >>> text = "testing Colleges blogger"
    • >>> print(text.capitalize())
    • output: Testing colleges blogger
  4. str.title(): Capitalizes the first character of each word in the string.
    • Example:
    • text = "testing Colleges blogger"
    • >>> print(text.title())
    • output: Testing Colleges Blogger
  5. str.strip(): Removes leading and trailing whitespace.
    • Example:
    • >>> text = "   Testing Colleges   "
    • >>> print(text.strip())
    • >>> output: "Testing Colleges"
  6. str.lstrip(): Removes leading whitespace.
    • Example:
    • >>> text = "   Testing Colleges   "
    • >>> print(text.lstrip())
    • >>> output: "Testing Colleges   "
  7. str.rstrip(): Removes trailing whitespace.
    • Example:
    • >>> text = "   Testing Colleges   "
    • >>> print(text.rstrip())
    • >>> output: "   Testing Colleges"
  8. str.replace(old, new): Replaces occurrences of the "old" substring with the "new" substring.
    • Example:
    • >>> text = "I love Java"
    • >>> new_text = text.replace("Java", "Python")
    • >>> print(new_text)
    • output: "I love Python"
  9. str.split(separator): Splits a string into a list of substrings based on the specified separator.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> print(text.split("e"))
    • output: ['T', 'sting Coll', 'g', 's']
  10. str.join(iterable): Joins the elements of an iterable (e.g., a list) into a single string using the specified separator.
    • Example:
    • >>> l = ['apple', 'banana', 'mango']
    • >>> text = ', '.join(l)
    • >>> print(text)
    • output: "apple, banana, mango"
  11. str.startswith(prefix): Checks if the string starts with the specified prefix.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> s = text.startswith("Testing")
    • >>> print(s)
    • output: True
  12. str.endswith(suffix): Checks if the string ends with the specified suffix.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> s = text.endswith("Colleges")
    • >>> print(s)
    • output: True
  13. str.find(substring): Searches for the first occurrence of a substring and returns its index.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> n = text.find("Colleges")
    • >>> print(n)
    • output: 8
  14. str.rfind(substring): Searches for the last occurrence of a substring and returns its index.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> print(text.rfind("e"))
    • output: 14
  15. str.index(substring): Similar to find(), but raises an exception if the substring is not found.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> print(text.find("a"))
    • >>> output: -1
    • >>> print(text.index("a"))
    • >>> output: ValueError: substring not found
  16. str.count(substring): Counts the number of non-overlapping occurrences of a substring.
    • Example:
    • >>> text = "Peter Piper picked a peck of pickled peppers."
    • >>> n = text.count("peck")
    • >>> print(n)
    • output: 1
  17. str.isalpha(): Checks if all characters in the string are alphabetic.
    • Example:
    • >>> text = "Testing Colleges"
    • >>> text2 = "TestingColleges"
    • >>> print(text.isalpha())
    • output: False
    • >>> print(text2.isalpha())
    • output: True
  18. str.isnumeric(): Checks if all characters in the string are numeric.
    • Example:
    • >>> text = "12345"
    • >>> print(text.isnumeric)
    • output: True
  19. str.isalnum(): Checks if all characters in the string are alphanumeric.
    • Example:
    • >>> text = "1a2345"
    • >>> print(text.isalnum())
    • output: True
  20. str.isdigit(): Checks if all characters in the string are digits.
    • Example:
    • >>> print("1234".isdigit())   # True
    • >>> print("123.456".isdigit())   # False
    • >>> print("1,234,567".isdigit())   # False
    • >>> print("".isdigit())   # False
    • -----------------------------------
    • >>> print("1234".isnumeric())   # True
    • >>> print("123.456".isnumeric())   # True
    • >>> print("1,234,567".isnumeric())   # True
    • >>> print("".isnumeric())   # False
  21. str.islower(): Checks if all characters in the string are lowercase letters.
    • Example:
    • >>> text = "testing colleges"
    • >>> print(text.islower())
    • output: True
  22. str.isupper(): Checks if all characters in the string are uppercase letters.
    • Example:
    • >>> text = "TESTING COLLEGES"
    • >>> print(text.isupper())
    • output: True
These are just a few of the many string methods available in Python. You can explore more string methods and their usage in the Python documentation:

No comments:

Post a Comment