In this example we will show how to change the case of a string in Python.
2. Methods
upper()
method converts all lowercase letters in a string to uppercase.
lower()
method converts all uppercase letters in the string to lowercase.
capitalize()
changes the first letter of the string to uppercase, and the other letters to lowercase. This method returns a string with initial capitalization.
title()
method returns a “titled” string, which means that the first letter of all words is converted to uppercase, and the rest of the letters are lowercase.
Source Code
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
Str = input('Please enter a string:')
print(Str)
print('Output:')
# Convert all lowercase letters in the string to uppercase
print(Str.upper())
# Convert all uppercase letters in the string to lowercase
print(Str.lower())
# Convert the first letters of the string to uppercase
print(Str.capitalize())
# Convert the first letters of each word to uppercase
print(Str.title())
Output:
Please enter a string:
my major is Computer Science
Output:
MY MAJOR IS COMPUTER SCIENCE
my major is computer science
My major is computer science
My Major Is Computer Science