TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python String | capitalize() method with Examples

Python String capitalize() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.

<< Back to PYTHON

Python String capitalize() Method

Python capitalize() method converts first character of the string into uppercase without altering the whole string. It changes the first character only and skips rest of the string unchanged.

Signature

capitalize()

Parameters

No parameter is required.

Return Type

It returns a modified string.

Python String Capitalize() Method Example 1

# Python capitalize() function example
# Variable declaration
str = "TheDeveloperBlog"
# Calling function
str2 = str.capitalize()
# Displaying result
print("Old value:", str)
print("New value:", str2)

Output:

Old value: TheDeveloperBlog
New value: TheDeveloperBlog

Python String Capitalize() Method Example 2

What if first character already exist in uppercase.

# Python capitalize() function example
# Variable declaration
str = "TheDeveloperBlog"
# Calling function
str2 = str.capitalize()
# Displaying result
print("Old value:", str)
print("New value:", str2)

Output:

Old value: TheDeveloperBlog	
New value: TheDeveloperBlog

It returns the same string without any alteration.

Python String Capitalize() Method Example 3

What if first character is a digit or non-alphabet character? This method does not alter character if it is other than a string character.

# Python capitalize() function example
# Variable declaration
str = "#TheDeveloperBlog"
# Calling function
str2 = str.capitalize()
# Displaying result
print("Old value:", str)
print("New value:", str2)
print("--------digit---------")
str3 = "1-TheDeveloperBlog"
str4 = str3.capitalize()
print("Old value:", str3)
print("New value:", str4)

Output:

Old value: #TheDeveloperBlog
New value: #TheDeveloperBlog
--------digit---------
Old value: 1-TheDeveloperBlog
New value: 1-TheDeveloperBlog

Next TopicPython Strings




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf