String Methods

·

3 min read

String Methods

Python provides a set of built-in string methods that we can use to modify and manipulate strings. Here is a list of some of the most commonly used built-in string methods in Python:

upper(): The upper() method converts a string to uppercase.

Syntax:

string.upper()

Example:

name = "john"
print(name.upper()) 
 # Output: JOHN

lower(): The lower() method converts a string to lowercase.

Syntax:

string.lower()

Example:

name = "JANE"
print(name.lower())  
# Output: jane

strip(): The strip() method removes any white spaces before and after the string.

Syntax:

string.strip()

Example:

string_with_spaces = "  hello  "
print(string_with_spaces.strip())  
# Output: "hello"

rstrip(): The rstrip() method removes any trailing characters.

Syntax:

string.rstrip()

Example:

string_with_trailing_characters = "hello!!!"
print(string_with_trailing_characters.rstrip("!"))  
# Output: "hello"

replace(): The replace() method replaces all occurrences of a string with another string.

Syntax:

string.replace(old_value, new_value)

Example:

string_with_replacement = "hello world"
print(string_with_replacement.replace("world", "universe"))  
# Output: "hello universe"

split(): The split() method splits the given string at the specified instance and returns the separated strings as list items.

Syntax:

string.split(separator)

Example:

string_to_split = "apple,banana,orange"
print(string_to_split.split(","))  # Output: ["apple", "banana", "orange"]

capitalize(): The capitalize() method turns only the first character of the string to uppercase and the rest of the characters to lowercase.

Syntax:

string.capitalize()

Example:

name = "jane DOE"
print(name.capitalize())  # Output: "Jane doe"

count(): The count() method returns the number of times the given value has occurred within the given string.

Syntax:

string.count(value)

Example:

string_to_count = "apple,banana,orange,apple"
print(string_to_count.count("apple"))  
# Output: 2

find(): The find() method searches for the first occurrence of the given value and returns the index where it is present. If the given value is absent from the string, it returns -1.

Syntax:

string.find(value)

Example:

string_to_find = "hello world"
print(string_to_find.find("world"))  # Output: 6

center(): The center() method aligns the string to the center as per the parameters given by the user.

Syntax:

string.center(length, character)

Example:

string_to_center = "hello"
print(string_to_center.center(10, "-"))  
# Output: "--hello---"

title(): The title() method capitalizes each letter of the word within the string.

Syntax:

string.title()

Example:

string_to_title = "hello world"
print(string_to_title.title())  
# Output: "Hello World"

swapcase(): The swapcase() method changes the character casing of the string. Upper case characters are converted to lower case and lower case characters to upper case.

Syntax:

string.swapcase()

And here is an example of how to use it in Python:

original_string = "tHiS iS a StRiNg tO sWaPcAsE"
swapped_string = original_string.swapcase()

print("Original string: ", original_string)
print("Swapped string: ", swapped_string)

Output:

Original string: tHiS iS a StRiNg tO sWaPcAsE
Swapped string: ThIs Is A sTrInG To SwApCaSe