Program for Python String capitalize() Function

In the previous article, we have discussed Python Program for hypot() Function
capitalize() Function in Python:

The capitalize() method returns a string in which the first character is upper case and the remaining characters are lower case.

Non-letters are not capitalized by the string capitalize function.

Syntax:

string.capitalize()

Parameters: This function doesn’t have any parameters.

Examples:

Example1:

Input:

Given first string = "hello this is BTECHGEEKS"
Given second string = "10345 good morning btechgeeks"

Output:

The above given first string is : hello this is BTECHGEEKS
The given first string after applying capitalize() function: Hello this is btechgeeks
The above given second string is : 10345 good morning btechgeeks
The given second string after applying capitalize() function: 10345 good morning btechgeeks

Example2:

Input:

Given first string = "heLLO 1256 GOOd MOrning"
Given second string = "Hello btech137GEEKS"

Output:

The above given first string is : heLLO 1256 GOOd MOrning
The given first string after applying capitalize() function: Hello 1256 good morning
The above given second string is : Hello btech137GEEKS
The given second string after applying capitalize() function: Hello btech137geeks

Program for capitalize() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first string as static input and store it in a variable.
  • Apply capitalize() function to the given first string to get the string in which the first character is upper case and the remaining characters are lower case.
  • Store it in another variable.
  • Print the above-given String.
  • Print the above-given String after applying capitalize() function.
  • Similarly, do the same for other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
gvn_fststr = "hello this is BTECHGEEKS"
# Apply capitalize() function to the given first string to get the string in which
# the first character is upper case and the remaining characters are lower case.
# Store it in another variable.
capitize_str1 = gvn_fststr.capitalize()
# Print the above given String
print("The above given first string is :", gvn_fststr)
# Print the above given String after applying capitalize() function.
print("The given first string after applying capitalize() function:", capitize_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = "10345 good morning btechgeeks"
capitize_str2 = gvn_scndstr.capitalize()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying capitalize() function:", capitize_str2)

Output:

The above given first string is : hello this is BTECHGEEKS
The given first string after applying capitalize() function: Hello this is btechgeeks
The above given second string is : 10345 good morning btechgeeks
The given second string after applying capitalize() function: 10345 good morning btechgeeks

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first string as user input using the input() function and store it in a variable.
  • Apply capitalize() function to the given first string to get the string in which the first character is upper case and the remaining characters are lower case.
  • Store it in another variable.
  • Print the above-given String.
  • Print the above-given String after applying capitalize() function.
  • Similarly, do the same for other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the first string as user input using the input() function and store it in a variable.
gvn_fststr = input("Enter some Random String = ")
# Apply capitalize() function to the given first string to get the string in which
# the first character is upper case and the remaining characters are lower case.
# Store it in another variable.
capitize_str1 = gvn_fststr.capitalize()
# Print the above given String
print("The above given first string is :", gvn_fststr)
# Print the above given String after applying capitalize() function.
print("The given first string after applying capitalize() function:", capitize_str1)
# Similarly do the same for other string and print the result string.
gvn_scndstr = input("Enter some Random String = ")
capitize_str2 = gvn_scndstr.capitalize()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying capitalize() function:", capitize_str2)

Output:

Enter some Random String = heLLO 1256 GOOd MOrning
The above given first string is : heLLO 1256 GOOd MOrning
The given first string after applying capitalize() function: Hello 1256 good morning
Enter some Random String = Hello btech137GEEKS
The above given second string is : Hello btech137GEEKS
The given second string after applying capitalize() function: Hello btech137geeks

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.