Capitalize in Python

Published on 09 October 2019 (Updated: 15 May 2023)

Welcome to the Capitalize in Python page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

import sys


def capitalize(input):
    if len(input) > 0:
        print(input[0].capitalize() + input[1:])


if __name__ == '__main__':
    if(len(sys.argv) == 1 or len(sys.argv[1]) == 0):
        print('Usage: please provide a string')
    else:
        capitalize(sys.argv[1])

Capitalize in Python was written by:

This article was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

We will consider this code block by block on the order of execution.

if __name__ == '__main__':
    if(len(sys.argv) == 1 or len(sys.argv[1]) == 0):
        print('Usage: please provide a string')
    else:
        capitalize(sys.argv[1])

This code checks if the main module is run. If it is, it then checks if the length of argument string provided by the user is 1 or empty string. If it is, it then prints correct usage pattern. Otherwise, it passes control to capitalize function passing argument string provided by the user.

def capitalize(input):
    if len(input) > 0:
        print(input[0].capitalize() + input[1:])

If the length provided by the user string is greater than 0, then it prints first letter of the string capitalized and concatenates the rest of the string.

How to Run the Solution

Capitalize in Python. After that, we should make sure we have the latest Python interpreter. From there, we can simply run the following command in the terminal:

python capitalize.py

Alternatively, we can copy the solution into an online Python interpreter and hit run.