Python’s built-in functions for string manipulation

Alright, Python’s built-in functions for string manipulation! These are some seriously awesome tools that can help you do all sorts of cool stuff with strings. And the best part? They’re already built into Python, so you don’t have to go searching for them in a library or something.

First up, we have capitalize(). This function takes a string and returns it with the first letter capitalized. For example:

# This script demonstrates the use of the capitalize() function in Python to capitalize the first letter of a string.

# First, we define a string variable called "word" and assign it the value "hello".
word = "hello"

# Next, we use the capitalize() function on the "word" variable and assign the result to a new variable called "capitalized_word".
capitalized_word = word.capitalize()

# Finally, we print the value of the "capitalized_word" variable to the console.
print(capitalized_word)

# Output: 'Hello'

Next, join(). This function allows you to combine multiple strings into one big string using a specified separator. Here’s an example:

# This script uses the join() function to combine multiple strings into one big string using a specified separator.

# First, we import the necessary module, in this case, the "string" module.
import string

# Next, we define a list of strings that we want to join together.
strings = ["item1", "item2", "item3"]

# Then, we use the join() function to combine the strings in the list, using a comma and space as the separator.
combined_string = ", ".join(strings)

# Finally, we print the result.
print(combined_string)

# Output: 'item1, item2, item3'

Now let’s say you have a string with some whitespace at the beginning and end that you want to remove. You can use strip() for this! This function removes any leading or trailing whitespace from a string:

# This script removes any leading or trailing whitespace from a string using the strip() function.

# First, we define a string with some whitespace at the beginning and end.
string = "   basket    "

# Then, we use the strip() function to remove the whitespace.
# The strip() function takes in no parameters and returns a new string with the whitespace removed.
# We assign the result to a variable called "clean_string".
clean_string = string.strip()

# Finally, we print the result to see the string without the whitespace.
print(clean_string)

# Output: 'basket'

What if you have multiple spaces in between words? No problem, Python has got your back. You can use replace() to replace all occurrences of one substring with another. Here’s an example:

# This script replaces all occurrences of spaces with tabs in a given string.

# Define a string with multiple spaces in between words
string = "I.am.aware.that.spaces.are.a.thing"

# Use the replace() method to replace all spaces with tabs
# The first argument is the substring to be replaced, and the second argument is the replacement substring
# The result is stored in a new string
new_string = string.replace(" ", "\t")

# Print the new string with tabs instead of spaces
print(new_string)

# Output: "I\tam\taware\tthat\tspaces\tare\ta\thing"

In this case, we’re replacing all spaces with a tab character (which is represented by the \t escape sequence). Pretty cool!

Now endswith() and startswith(). These functions check if a string ends or starts with a specified substring. Here are some examples:

# This script demonstrates the use of the endswith() and startswith() functions in Python.

# First, we import the necessary module, in this case, the string module.
import string

# Next, we define a string variable called "filename" and assign it the value "script.py".
filename = "script.py"

# Then, we use the endswith() function to check if the string "filename" ends with the specified substring ".py".
# The function returns a boolean value, which is then assigned to the variable "ends_with_py".
ends_with_py = filename.endswith(".py")

# We print the value of "ends_with_py" to see if the string "filename" does indeed end with ".py".
print(ends_with_py)

# Next, we use the "in" keyword to check if the substring "sc" is present in the string "filename".
# The "in" keyword returns a boolean value, which is then assigned to the variable "contains_sc".
contains_sc = "sc" in filename

# We print the value of "contains_sc" to see if the substring "sc" is present in the string "filename".
print(contains_sc)

# Finally, we use the startswith() function to check if the string "hello" starts with the specified substring "he".
# The function returns a boolean value, which is then assigned to the variable "starts_with_he".
starts_with_he = "hello".startswith("he")

# We print the value of "starts_with_he" to see if the string "hello" does indeed start with "he".
print(starts_with_he)

# Output:
# True
# True
# True

Finally, docstrings! Docstrings are a way to add documentation directly into your code. They can be used by tools like Sphinx or pydoc to generate documentation for your project. Here’s an example:

# This function is used to define a function named "my_function"
def my_function():
    """This function does something awesome.""" 
    # This is a docstring, used to add documentation to the function
    pass
    # This is a placeholder statement, it does nothing and should be removed

And that’s it! These are just a few of the many built-in functions available in Python for string manipulation.

SICORPS