Saturday, November 16, 2024
Google search engine
HomeLanguagesPython Program to Capitalize First Character of String

Python Program to Capitalize First Character of String

Python program to capitalize the first character of a string; Through this tutorial, you will learn how to capitalize the first character of a string in python.

.medrectangle-3-multi-320{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:250px;padding:0;text-align:center!important;width:100%}

Many times when you are working in Python or some other program langauge. So you have to do modification/manipulation etc. of strings. Today we will learn to capitalize strings first character or convert string first character/letter to uppercase in Python.

Python String capitalize()

Python capitalize method is one of the Python String Method, which is used to convert the first character into Uppercase and remaining characters to Lowercase and return a new string.

The syntax of capitalize() is:

string.capitalize()

capitalize() Parameter

The capitalize() function doesn’t take any parameter.

Return Value from capitalize()

The capitalize() function returns a string with first letter capitalized and all other characters lowercased. It doesn’t modify the original string.

Now you know that How to convert first character/letter of string to uppercase in Python. So now we discuss how to write a capitalize Function in Python programs with an example:

Example 1: Capitalize a Sentence

string = "python is AWesome."

capitalized_string = string.capitalize()

print('Old String: ', string)
print('Capitalized String:', capitalized_string)

Example 2: Non-alphabetic First Character

string = "+ is an operator."

new_string = string.capitalize()

print('Old String:', string)
print('New String:', new_string)

Recommended Python Posts

  1. Python Strings Example Tutorial
  2. Tuple Python with Example
  3. Python Modules: Learn Modules In Python
  4. Python Package (Create and Import) Example
  5. Built-In String Methods/Functions in Python With Example
RELATED ARTICLES

Most Popular

Recent Comments