Write a python program to capitalize the first and last letters of each word of a given string

Example program to capitalize first and last letters of each word of a given string in Python.

def capitalize_first_last_letters(str1):
    str1 = result = str1.title()
    result = ""
    for word in str1.split():
        result += word[:-1] + word[-1].upper() + " "
    return result[:-1]

print(capitalize_first_last_letters("python code and example"))


Python Capitalize first and last letters of each word of string

Another Example

Take input from the user and convert the first and last letter of string to Upper-Case in Python.

First, do slicing the String convert 1st character and convert it to upper case and Concatenate the string with remaining-1 length. The last step takes last character and change it to uppercase and concatenates it to string.

String = input('Enter the String :')
String = String[0:1].upper() + String[1:len(String)-1] + String[len(String)-1:len(String)].upper()



Do comment if you have any doubts and suggestions on this python code.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.