Sobes.tech
Junior

What methods are available for working with strings in your programming language?

sobes.tech AI

Answer from AI

Python provides a variety of built-in methods for working with strings. Here are the main ones:

  • Case conversion:

    • lower(): Converts all characters in the string to lowercase.
    • upper(): Converts all characters in the string to uppercase.
    • capitalize(): Converts the first character of the string to uppercase, the rest to lowercase.
    • title(): Converts the first character of each word in the string to uppercase.
    • swapcase(): Swaps the case of each character in the string (uppercase to lowercase, lowercase to uppercase).
  • Search and replace:

    • find(substring[, start[, end]]): Finds the first occurrence of a substring and returns its index. Returns -1 if not found.
    • index(substring[, start[, end]]): Similar to find(), but raises a ValueError if not found.
    • rfind(substring[, start[, end]]): Finds the last occurrence of a substring starting from the end.
    • rindex(substring[, start[, end]]): Similar to rfind(), but raises a ValueError if not found.
    • count(substring[, start[, end]]): Counts the number of non-overlapping occurrences of a substring.
    • replace(old, new[, count]): Replaces all (or up to count) occurrences of a substring with a new string.
  • Splitting and joining:

    • split(sep=None, maxsplit=-1): Splits the string into a list of substrings based on the separator sep. If sep is None, splits on whitespace.
    • splitlines([keepends]): Splits the string into a list of lines.
    • join(iterable): Joins elements of an iterable into a single string with the string as separator.
  • Content checks:

    • startswith(prefix[, start[, end]]): Checks if the string starts with a prefix.
    • endswith(suffix[, start[, end]]): Checks if the string ends with a suffix.
    • isalnum(): Checks if the string is alphanumeric.
    • isalpha(): Checks if the string contains only alphabetic characters.
    • isdigit(): Checks if the string contains only digits.
    • isspace(): Checks if the string contains only whitespace.
    • islower(): Checks if all alphabetic characters are lowercase.
    • isupper(): Checks if all alphabetic characters are uppercase.
    • istitle(): Checks if the string is in title case.
  • Removing spaces:

    • strip([chars]): Removes leading and trailing whitespace or specified characters.
    • lstrip([chars]): Removes leading whitespace or specified characters.
    • rstrip([chars]): Removes trailing whitespace or specified characters.
  • Formatting:

    • format(*args, **kwargs): Formats the string, replacing placeholders with arguments.
    • f-strings: Allow embedding variables and expressions directly in strings, prefixed with f.

Example usage of some methods:

s = "   Hello, World!   "
print(s.strip())  # Output: "Hello, World!"
print(s.lower())  # Output: "   hello, world!   "
print(s.replace("World", "Python"))  # Output: "   Hello, Python!   "

words = ["Привет", "мир"]
print(" ".join(words)) # Output: "Привет мир"

name = "Alice"
age = 30
print(f"Меня зовут {name}, и мне {age} лет.") # Output: "Меня зовут Alice, и мне 30 лет."