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:
- str.upper(): Converts a string to uppercase.
- Example:
- >>> text = "Testing Colleges"
- >>> print(text.upper())
- >>> output: TESTING COLLEGES
- str.lower(): Converts a string to lowercase.
- Example:
- >>> text = "Testing Colleges"
- >>> print(text.lower())
- >>> output: testing colleges
- str.capitalize(): Capitalizes the first letter of the string.
- Example:
- >>> text = "testing Colleges blogger"
- >>> print(text.capitalize())
- output: Testing colleges blogger
- str.title(): Capitalizes the first character of each word in the string.
- Example:
- text = "testing Colleges blogger"
- >>> print(text.title())
- output: Testing Colleges Blogger
- str.strip(): Removes leading and trailing whitespace.
- Example:
- >>> text = " Testing Colleges "
- >>> print(text.strip())
- >>> output: "Testing Colleges"
- str.lstrip(): Removes leading whitespace.
- Example:
- >>> text = " Testing Colleges "
- >>> print(text.lstrip())
- >>> output: "Testing Colleges "
- str.rstrip(): Removes trailing whitespace.
- Example:
- >>> text = " Testing Colleges "
- >>> print(text.rstrip())
- >>> output: " Testing Colleges"
- 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"
- 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']
- 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"
- str.startswith(prefix): Checks if the string starts with the specified prefix.
- Example:
- >>> text = "Testing Colleges"
- >>> s = text.startswith("Testing")
- >>> print(s)
- output: True
- str.endswith(suffix): Checks if the string ends with the specified suffix.
- Example:
- >>> text = "Testing Colleges"
- >>> s = text.endswith("Colleges")
- >>> print(s)
- output: True
- 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
- 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
- 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
- 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
- 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
- str.isnumeric(): Checks if all characters in the string are numeric.
- Example:
- >>> text = "12345"
- >>> print(text.isnumeric)
- output: True
- str.isalnum(): Checks if all characters in the string are alphanumeric.
- Example:
- >>> text = "1a2345"
- >>> print(text.isalnum())
- output: True
- 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
- str.islower(): Checks if all characters in the string are lowercase letters.
- Example:
- >>> text = "testing colleges"
- >>> print(text.islower())
- output: True
- 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