Mastering Python Strings: A Comprehensive Guide to String Methods

ยท

6 min read

In Python, strings are objects, and they have many built-in methods that you can use to manipulate them. These methods are called string methods.

you might like one of my other writing :

From Netflix to Hulu: How Machine Learning is Revolutionizing Streaming Services

Here are some common string methods:

  • lower(): Returns the string in lowercase.

  • upper(): Returns the string in uppercase.

  • capitalize(): Capitalizes the first character of the string.

  • strip(): Removes leading and trailing whitespace from the string.

  • replace(): Replaces a specified substring with another substring.

  • split(): Splits the string into a list of substrings based on a specified delimiter.

  • format(): Formats the string using a specified format.

lower()

Here's an example of how to use the lower() method in Python:

string = "Hello World!"

# Use the lower() method to return the string in lowercase
lowercase_string = string.lower()

# Print the result
print(lowercase_string) # Output: "hello world!"

In the example above, we first create a string called string with the value "Hello World!". Then we call the lower() method on this string and save the result to a new variable called lowercase_string. Finally, we print the value of lowercase_string, which is "hello world!".

upper()

Here's an example of how to use the upper() method in Python:

string = "Hello World!"

# Use the upper() method to return the string in uppercase
uppercase_string = string.upper()

# Print the result
print(uppercase_string) # Output: "HELLO WORLD!"

In the example above, we first create a string called string with the value "Hello World!". Then we call the upper() method on this string and save the result to a new variable called uppercase_string. Finally, we print the value of uppercase_string, which is "HELLO WORLD!".

capitalize()

Here's an example of how to use the capitalize() method in Python:

string = "hello world!"

# Use the capitalize() method to capitalize the first character of the string
capitalized_string = string.capitalize()

# Print the result
print(capitalized_string) # Output: "Hello world!"

In the example above, we first create a string called string with the value "hello world!". Then we call the capitalize() method on this string and save the result to a new variable called capitalized_string. Finally, we print the value of capitalized_string, which is "Hello world!". Notice that the capitalize() method only capitalizes the first character of the string and leaves the rest of the string unchanged.

strip()

Here's an example of how to use the strip() method in Python:

string = "   Hello World!   "

# Use the strip() method to remove leading and trailing whitespace from the string
stripped_string = string.strip()

# Print the result
print(stripped_string) # Output: "Hello World!"

In the example above, we first create a string called string with the value " Hello World! ". This string has a leading and trailing whitespace (spaces and tabs) at the beginning and end of the string. Then we call the strip() method on this string and save the result to a new variable called stripped_string. Finally, we print the value of stripped_string, which is "Hello World!". Notice that the strip() method removes the leading and trailing whitespace from the string, but leaves the whitespace within the string unchanged.

replace()

Here's an example of how to use the replace() method in Python:

string = "Hello World!"

# Use the replace() method to replace "World" with "Universe"
replaced_string = string.replace("World", "Universe")

# Print the result
print(replaced_string) # Output: "Hello Universe!"

In the example above, we first create a string called string with the value "Hello World!". Then we call the replace() method on this string and pass it two arguments: the substring to be replaced ("World"), and the replacement substring ("Universe"). The replace() method returns a new string with the specified substring replaced, and we save this result to a new variable called replaced_string. Finally, we print the value of replaced_string, which is "Hello Universe!". Notice that the replace() method only replaces the first occurrence of the specified substring. If you want to replace all occurrences of the substring, you can use the replace() method in a loop.

split()

Here's an example of how to use the split() method in Python:

string = "Hello, World!"

# Use the split() method to split the string into a list of substrings based on the comma delimiter
string_list = string.split(",")

# Print the result
print(string_list) # Output: ["Hello", " World!"]

In the example above, we first create a string called string with the value "Hello, World!". This string has a comma as a delimiter, which we will use to split the string into a list of substrings. Then we call the split() method on this string and pass it the delimiter (in this case, a comma) as an argument. The split() method returns a list of substrings, and we save this result to a new variable called string_list. Finally, we print the value of string_list, which is ["Hello", " World!"]. Notice that the split() method splits the string at every occurrence of the specified delimiter and returns a list of the resulting substrings.

format()

Here's an example of how to use the format() method in Python:

string = "Hello, {}! Today is {}."

# Use the format() method to insert values into the string
formatted_string = string.format("John", "Monday")

# Print the result
print(formatted_string) # Output: "Hello, John! Today is Monday."

In the example above, we first create a string called string with the value "Hello, {}! Today is {}.". This string has two placeholders ({}) where we can insert values. Then we call the format() method on this string and pass it two arguments: the values to be inserted into the placeholders ("John" and "Monday"). The format() method returns a new string with the placeholders replaced by the specified values, and we save this result to a new variable called formatted_string. Finally, we print the value of formatted_string, which is "Hello, John! Today is Monday.".

Here's another example of how to use the format() method to format a string with placeholders:

string = "The price of this item is ${:.2f}."

# Use the format() method to insert a value into the string
formatted_string = string.format(49.95)

# Print the result
print(formatted_string) # Output: "The price of this item is $49.95."

In the example above, we create a string called string with the value "The price of this item is ${:.2f}.". This string has a placeholder ({:.2f}) where we can insert a floating-point value. Then we call the format() method on this string and pass it the value to be inserted into the placeholder (49.95). The format() method returns a new string with the placeholder replaced by the specified value, formatted with two decimal places. We save this result to a new variable called formatted_string, and finally we print the value of formatted_string, which is "The price of this item is $49.95.".

If you liked this article, be sure to share it with your friends and followers. Thanks for reading!

That's all for now, folks! Thanks for stopping by, and we hope to see you again soon.

ย