string capitalize() in Python

PythonServer Side ProgrammingProgramming

In this tutorial, we are going to learn about capitalize method of strings. Let's dive into the tutorial.

The method capitalize will make the first letter of the string to capital. If the first letter is nonalphabet or capital letter, then there won't be any effect.

Follow the below steps to write the program.

  • Initialize a string.
  • Use the capitalize method to convert the first letter of string to a capital.
  • Store the result in a new variable.
  • Print the result.

Example

 Live Demo

# initializing the string
string = "tutorialspoint"
# capitalizing the first letter
result = string.capitalize()
# printing the result
print(result)

Output

If you run the above code, then you will get the following result.

Tutorialspoint

Now, change the first letter of the string in the previous example to capital. And run it again.

Example

 Live Demo

# initializing the string
string = "Tutorialspoint"
# capitalizing the first letter
result = string.capitalize()
# printing the result
print(result)

Output

If you run the above code, then you will get the following result.

Tutorialspoint

Add a non-alphabet as the first letter of the string in the previous example. And run it.

Example

# initializing the string
string = "tutorialspoint"
# capitalizing the first letter
result = string.capitalize()
# printing the result
print(result)
tutorialspoint

Output

If you run the above code, then you will get the following result.

tutorialspoint

What happens if you have multiple words and multiple lines in a string? The capitalize method will make only the first letter of string to capital.

Run the following code and see it.

Example

 Live Demo

# initializing the string
string = "tutorialspoint is great website to learn new things. and it's free"
# capitalizing the first letter
result = string.capitalize()
# printing the result
print(result)

Output

If you run the above code, then you will get the following result.

Tutorialspoint is great website to learn new things. and it's free

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

raja
Published on 11-Jul-2020 07:03:52
Advertisements