String functions for revision in Python: Let us Revise what we did learn!!

Essential String Functions in Python:

  1. len(string): Returns the length of the string (number of characters).
    • Example: length = len("Hello, World!") # length will be 13
  2. string.upper(): Converts all lowercase letters in the string to uppercase.
    • Example: uppercase_text = "hello".upper() # uppercase_text will be "HELLO"
  3. string.lower(): Converts all uppercase letters in the string to lowercase.
    • Example: lowercase_text = "HELLO".lower() # lowercase_text will be "hello"
  4. string.split(sep, maxsplit=None): Splits the string into a list of substrings based on the specified separator (sep). The optional maxsplit parameter limits the number of splits to occur.
    • Example: words = "apple,banana,cherry".split(",") # words will be ["apple", "banana", "cherry"]
  5. string.join(iterable): Joins elements from an iterable (e.g., list) into a single string using the specified separator.
    • Example: joined_string = "-".join(["apple", "banana", "cherry"]) # joined_string will be "apple-banana-cherry"
  6. string.strip(chars=None): Removes leading and trailing characters from the string. Optionally, you can specify characters to remove (chars).
    • Example: clean_text = " Extra spaces ".strip() # clean_text will be "Extra spaces"
  7. string.replace(old, new, count=None): Replaces occurrences of a substring (old) with another substring (new). The optional count parameter limits the number of replacements.
    • Example: fixed_text = "Mississippi".replace("ss", "s", 1) # fixed_text will be "Misispippi"
  8. string.startswith(prefix, start=0, end=None): Checks if the string starts with the specified prefix within a given range.
    • Example: does_start = "Hello, World!".startswith("Hello") # does_start will be True
  9. string.endswith(suffix, start=None, end=endobj): Checks if the string ends with the specified suffix within a given range.
    • Example: does_end = "Hello, World!".endswith("World!") # does_end will be True
  10. string.find(sub, start=0, end=None): Returns the index of the first occurrence of the substring (sub) within a given range, or -1 if not found.
  • Example: first_index = "Hello, World!".find("W") # first_index will be 7
  1. string.rfind(sub, start=0, end=None): Returns the index of the last occurrence of the substring (sub) within a given range, or -1 if not found.
  • Example: last_index = "Hello, World! World!".rfind("World") # last_index will be 17
  1. string.isalpha(): Checks if all characters in the string are alphabetic (letters a-z and A-Z).
  • Example: is_alpha = "hello123".isalpha() # is_alpha will be False
  1. string.isdigit(): Checks if all characters in the string are digits (0-9).
  • Example: is_digit = "12345".isdigit() # is_digit will be True
  1. string.isalnum(): Checks if all characters in the string are alphanumeric (letters and digits).
  • Example: is_alnum = "hello123".isalnum() # is_alnum will be True
  1. string.isspace(): Checks if all characters in the string are whitespace characters (spaces, tabs, newlines, etc.).
  • Example: is_space = " tn".isspace() # is_space will be True
  1. string.istitle(): Checks if the string is a titlecased string (first letter of each word uppercase, others lowercase).
  • Example: `is_title

Discover more from HintsToday

Subscribe to get the latest posts sent to your email.

Pages ( 8 of 8 ): « Previous1 ... 67 8

Discover more from HintsToday

Subscribe now to keep reading and get access to the full archive.

Continue reading