Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python String Modify


String Modify

Python has some built-in functions, and by using them we can modify the string.


Upper Function

The upper() function returns the string in upper case.

Example
message = "Hello world"
print(message.upper())
HELLO WORLD

You can see that all characters in the "Hello world" string characters were converted to uppercase letters.


Lower Function

The lower() function returns the string in lower case.

Example
message = "HELLO WORLD"
print(message.lower())
hello world

You can see that all characters in the "HELLO WORLD" string characters were converted to lowercase letters.


Remove Whitespace

The whitespace is the space before and after the actual string text, and by using the strip function, you can remove those white spaces.

Example
message = " Hello world "
print(message.strip())
Hello world

You can see in this example that the white spaces have been removed from the string text.


Replace string

Using the replace() method, you can replace a string with another string.

Example
message = "Hello world"
print(message.replace("Hello", "Welcome"))
Welcome world

In this example, we replaced the Hello text with Welcome by using the replace function.


Split String

The split() method splits a string text and returns a list with string words.

Example
message = "Hello world"
print(message.split(" "))
['Hello', 'world']

In the example, the string Hello world was split using the split function, with a space between each word.