DEV Community

Brayan Potosi
Brayan Potosi

Posted on

Python String Methods

Python has many build-in methods, these methods belong to a data-type now i will to explain the most common string methods

capitalize = this method returns some string but with the first character in uppercase.

my_string = 'random word'
capitalize_word = my_string.capitalize()
print(capitalize_word)

output : 'Random word'

Enter fullscreen mode Exit fullscreen mode

lower = this method returns a copy of the string with all characters in lowercase.

my_string = 'Random WORD'
lower_word = my_string.lower()
print(lower_word)

output : 'random word'

Enter fullscreen mode Exit fullscreen mode

upper = this method returns a copy of the string with all characters in uppercase.

my_string = 'Random WORD'
upper_word = my_string.upper()
print(upper_word)

output : 'RANDOM WORD'

Enter fullscreen mode Exit fullscreen mode

swapcase() = this method change the characters , if the character is uppercase change for lowercase, does the same for the opposite case.

my_string = 'RANDOM word'
swap_word = my_string.swapcase()
print(swap_word)

output : 'random WORD'

Enter fullscreen mode Exit fullscreen mode

Top comments (0)