A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
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.
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.
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.
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.