The Wayback Machine - https://web.archive.org/web/20240916092313/https://www.geeksforgeeks.org/string-capitalize-python/
Open In App

String capitalize() Method in Python

Last Updated : 05 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Python String capitalize() method returns a copy of the original string and converts the first character of the string to a capital (uppercase) letter while making all other characters in the string lowercase letters.

Example

Python
# initializing a string
name = "draco malfoy"
#using capitalize function
print(name.capitalize())

Output
Draco malfoy

Definition of capitalize() Function

The capitalize() function in Python is used to capitalize the first letter of a string. It turns the rest of all letters to lowercase.

If the first letter is not an alphabet, capitalize() function will leave it as it is and converts rest of the characters to small case.

capitalize() Method Syntax

string_name.capitalize()

Parameter

The capitalize() function does not take any parameter. 

Return

The capitalize() function returns a string with the first character in the capital.

capitalize() Method Examples

Let’s look at some capitalize in Python examples, to understand the function better.

Python
name = "geeks FOR geeks"

print(name.capitalize())

Output:

Geeks for geeks

Example 1: capitalize() only capitalizes the first letter of a string and lowers all the remaining characters

Python
string = "roses are red"
print("Original string:", string)
print("After using capitalzie:", string.capitalize())

Output:

Original string: roses are red
After using capitalize: Roses are red

Example 2: Python String capitalize () Method Doesn’t Modify the Original String

Python String capitalize () Method creates and returns a copy of the original string after modifications.

Python
string = "geeks for geeks"
string_2 = string.capitalize()

print("New string after using capitalize():", string_2)

print("Original string:", string)

Output
New string after using capitalize(): Geeks for geeks
Original string: geeks for geeks

We have covered how to capitalize a string in Python using the capitalize () function. It is useful for data formatting.

Example 3: Python String capitalize () Method leave as it is if first letter is not an alphabet

Python
string1 = "$this is an Example StRing"
string2 = "this is an Example StRing"

# using capitalize function
print(string1.capitalize())  # Output : $this is an example string
print(string2.capitalize())  # Output : This is an example string

Output
$this is an example string
This is an example string

Related Article: String Methods


Previous Article
Next Article

Similar Reads

Python Capitalize First Letter of Every Word in String
In text manipulation in Python, capitalizing the first letter of every word in a string is a common and essential task. This operation enhances the visual presentation of text and is particularly useful when dealing with user input, titles, or any scenario where proper capitalization is desired. In this article, we will explore some approaches to c
3 min read
Capitalize Each String in a List of Strings in Python
In Python, manipulating strings is a common task, and capitalizing each string in a list is a straightforward yet essential operation. This article explores some simple and commonly used methods to achieve this goal. Each method has its advantages and use cases, providing flexibility for different scenarios. Capitalize Each String In A List Of Stri
3 min read
numpy.defchararray.capitalize() in Python
numpy.core.defchararray.multiply(arr, n): Capitalizes the first letter of string element-wise. Parameters: arr : array-like or string. Returns : Capitalized first letter of the string. Code #1: # Python Program illustrating # numpy.char.capitalize() method import numpy as np arr1 = ['eAAAaeAAAa', 'ttttdsttttds', 'AAtAAt'] arr2 = ['11sf', 'sdsf2', '
1 min read
Python | Pandas Series.str.capitalize()
Series.str.capitalize() used to capitalize the string elements in pandas series. Series is a type of data structure which is used as same as list in python. Series can contain the different types of data as we want to enter in the series like list. Syntax : Series.str.capitalize() Parameters: None Returns: Series/Index of objects To get the link to
2 min read
PyQt5 QSpinBox - Making text capitalize
In this article we will see how we can make the text of the spin box capitalize, in order to set the font use setFont method which takes QFont object as argument. In order to make the text i.e font capitalize, we have to get the QFont object of the spin box then make it capitalize then reassign it to the spin box. In order to do this we use setCapi
2 min read
PyQt5 QSpinBox – Checking if text is capitalize ?
In this article we will see how we can if the text of the spin box is capitalize or not, in order to set the font we use setFont method which takes QFont object as argument. In order to make the text i.e font capitalize, we have to get the QFont object of the spin box then make it capitalize then reassign it to the spin box. In order to do this we
2 min read
Difference between str.capitalize() VS str.title()
Both title() and capitalize() have similar functionality of capitalizing first characters. Let us see the difference between the two of them. Method 1: title() title() function in Python is the Python String Method which is used to convert the first character in each word to Uppercase and the remaining characters to Lowercase in the string and retu
3 min read
Capitalize first letter of a column in Pandas dataframe
Analyzing real-world data is somewhat difficult because we need to take various things into consideration. Apart from getting the useful data from large datasets, keeping data in required format is also very important. One might encounter a situation where we need to capitalize any specific column in given dataframe. Let's see how can we capitalize
2 min read
Class Method vs Static Method vs Instance Method in Python
Three important types of methods in Python are class methods, static methods, and instance methods. Each serves a distinct purpose and contributes to the overall flexibility and functionality of object-oriented programming in Python. In this article, we will see the difference between class method, static method, and instance method with the help o
5 min read
String slicing in Python to check if a string can become empty by recursive deletion
Given a string “str” and another string “sub_str”. We are allowed to delete “sub_str” from “str” any number of times. It is also given that the “sub_str” appears only once at a time. The task is to find if “str” can become empty by removing “sub_str” again and again. Examples: Input : str = "GEEGEEKSKS", sub_str = "GEEKS" Output : Yes Explanation :
2 min read
String slicing in Python to Rotate a String
Given a string of size n, write functions to perform following operations on string. Left (Or anticlockwise) rotate the given string by d elements (where d <= n).Right (Or clockwise) rotate the given string by d elements (where d <= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Left Rotation : "eksforGeeksGe" Right Rotation : "ksGeek
3 min read
Python | Sorting string using order defined by another string
Given two strings (of lowercase letters), a pattern and a string. The task is to sort string according to the order defined by pattern and return the reverse of it. It may be assumed that pattern has all characters of the string and all characters in pattern appear only once. Examples: Input : pat = "asbcklfdmegnot", str = "eksge" Output : str = "g
2 min read
String Alignment in Python f-string
Text Alignment in Python is useful for printing out clean formatted output. Some times the data to be printed varies in length which makes it look messy when printed. By using String Alignment the output string can be aligned by defining the alignment as left, right or center and also defining space (width) to reserve for the string. Approach : We
2 min read
String to Int and Int to String in Python
Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting a string to int and int to string. Converting a string to an int If we want to convert a number that is represented in the string to int, we have to use the int() function. This function is used
2 min read
Pad or fill a string by a variable in Python using f-string
f-string stands for formatted string. It had come up by Python Version 3.6 and rapidly used to do easy formatting on strings. F-string is a string literal having syntax starts with f and followed by {}. That placeholder used for holding variable, that will be changed upon the variable names and their values respectively. There are already strings f
4 min read
Convert Unicode String to a Byte String in Python
Python is a versatile programming language known for its simplicity and readability. Unicode support is a crucial aspect of Python, allowing developers to handle characters from various scripts and languages. However, there are instances where you might need to convert a Unicode string to a regular string. In this article, we will explore five diff
2 min read
Python string length | len() function to find string length
The string len() function returns the length of the string. In this article, we will see how to find the length of a string using the string len() method. Example: [GFGTABS] Python string = "Geeksforgeeks" print(len(string)) [/GFGTABS]Output13String len() Syntaxlen(string) ParameterString: string of which you want to find the length. Retu
5 min read
Python String Formatting - How to format String?
String formatting allows you to create dynamic strings by combining variables and values. In this article, we will discuss about 5 ways to format a string. You will learn different methods of string formatting with examples for better understanding. Let's look at them now! How to Format Strings in PythonThere are five different ways to perform stri
10 min read
Python String casefold() Method
Python String casefold() method is used to convert string to lowercase. It is similar to the Python lower() string method, but the case removes all the case distinctions present in a string. Python String casefold() Method Syntax Syntax: string.casefold() Parameters: The casefold() method doesn't take any parameters. Return value: Returns the case
1 min read
Python String isspace() Method
Python String isspace() method returns “True” if all characters in the string are whitespace characters, Otherwise, It returns “False”. This function is used to check if the argument contains all whitespace characters, such as: ‘ ‘ – Space‘\t’ – Horizontal tab‘\n’ – Newline‘\v’ – Vertical tab‘\f’ – Feed‘\r’ – Carriage returnPython String isspace()
2 min read
Python String isalpha() Method
Python String isalpha() method is used to check whether all characters in the String are an alphabet. Python String isalpha() Method SyntaxSyntax: string.isalpha() Parameters: isalpha() does not take any parameters Returns: True: If all characters in the string are alphabet.False: If the string contains 1 or more non-alphabets.Errors and Exceptions
4 min read
Python String isprintable() Method
Python String isprintable() is a built-in method used for string handling. The isprintable() method returns "True" if all characters in the string are printable or the string is empty, Otherwise, It returns "False". This function is used to check if the argument contains any printable characters such as: Digits ( 0123456789 )Uppercase letters ( ABC
3 min read
Python String isdigit() Method
Python String isdigit() method returns “True” if all characters in the string are digits, Otherwise, It returns “False”. Python String isdigit() Method Syntax Syntax: string.isdigit() Parameters: isdigit() does not take any parameters Returns: True - If all characters in the string are digits.False - If the string contains 1 or more non-digits. Tim
3 min read
Python String isnumeric() Method
The isnumeric() method is a built-in method in Python that belongs to the string class. It is used to determine whether the string consists of numeric characters or not. It returns a Boolean value. If all characters in the string are numeric and it is not empty, it returns “True” If all characters in the string are numeric characters, otherwise ret
3 min read
Python String splitlines() Method
Python String splitlines() method is used to split the lines at line boundaries. The function returns a list of lines in the string, including the line break(optional). Syntax: string.splitlines([keepends]) Parameters: keepends (optional): When set to True line breaks are included in the resulting list. This can be a number, specifying the position
2 min read
Python String center() Method
Python String center() method creates and returns a new string that is padded with the specified character. Syntax: string.center(length[, fillchar]) Parameters: length: length of the string after padding with the characters.fillchar: (optional) characters which need to be padded. If it's not provided, space is taken as the default argument. Return
2 min read
Python String istitle() Method
Python String istitle() Method is a built-in string function that returns True if all the words in the string are title cased, otherwise returns False. Python String istitle() Method Syntax Syntax: string.istitle() Returns: True if the string is a title-cased string otherwise returns False. Python String istitle() Method Example C/C++ Code string =
2 min read
Python string isdecimal() Method
Python String isdecimal() function returns true if all characters in a string are decimal, else it returns False. In this article, we will explore further the isdecimal() method, understand its functionality, and explore its practical applications in Python programming. Python String isdecimal() SyntaxSyntax: string_name.isdecimal(), string_name is
2 min read
Python String isalnum() Method
Python String isalnum() method checks whether all the characters in a given string are either alphabet or numeric (alphanumeric) characters. Python String isalnum() Method Syntax: Syntax: string_name.isalnum() Parameter: isalnum() method takes no parameters Return: True: If all the characters are alphanumeric False: If one or more characters are no
1 min read
Python string swapcase() Method
Python String swapcase() method converts all uppercase characters to lowercase and vice versa of the given string and returns it. Syntax: string_name.swapcase() Parameter: The swapcase() method does not take any parameter. Return Value: The swapcase() method returns a string with all the cases changed. Example Below is the Python implementation of
1 min read
Article Tags :
Practice Tags :