Make all letters capital python

Python String capitalize() Method

The Python String capitalize() method is used to capitalize the current string. It simply returns a string with the very first letter capitalized, that is in upper case and the remaining characters of that string are converted to lower case letters.

If the first letter of the input string is a non-alphabet or if it is already a capital letter, then there will be no effect in the output i.e. the original string is not modified.

We will be learning more details about the python string capitalize() method in this chapter.

Syntax

The following is the syntax for the python string capitalize() method.

Parameters

This method does not accept any parameters.

Return Value

This method returns the capitalized string as its output.

Example

Following is an example of the python string capitalize() function. In here, we are trying to capitalize the first letter of the string «tutorialspoint».

str = "tutorialspoint" output=str.capitalize() print("The resultant string is:", output)

On executing the above program, the following output is generated —

The resultant string is: Tutorialspoint.

Example

If the first letter of the input string is already a capital letter, then the capitalize() function returns the current string without any changes.

In the following example, we are creating a string with value «Tutorialspoint» as the first character of it is already a capital leter, if we invoke the capitalize() function on this string, it returns the current string without any changes.

str = "Hii! welcome to tutorialspoint." output=str.capitalize() print("The resultant string is:", output))

The following is the output obtained by executing the above program —

The resultant string is: Hii! welcome to tutorialspoint.

Example

If the first letter of the input string is not an alphabet, this function returns the original string.

In the following example, we are creating a string value with «$» as the first character and called the capitalize() function using this string. Since the starting letter is not an alphabet this function returns the current string without any changes.

str = "$Hii! welcome to tutorialspoint." output=str.capitalize() print("The resultant string is:", output)

The following output is obtained by executing the above program —

The resultant string is: $hii! welcome to tutorialspoint.

Example

The python string capitalize() function does not modify the original string. So, if you print it, the original string is printed without any modifications.

str = "hii! welcome to tutorialspoint." output=str.capitalize() print("The string after applying the capitalize() function is:", output) print("The origial string is:", str)

The above program, on executing, displays the following output —

The string after calling the capitalize() function: Hii! welcome to tutorialspoint. The origial string is: hii! welcome to tutorialspoint.

Example

The python string capitalize() function only capitalizes the very first letter. If the remaining characters in the string contain capital letters, they are all changed to small letters.

str = "hii! Welcome to TUTORIALSPOINT." output=str.capitalize() print("The string after applying the capitalize() function is:", output)

The output of the above program is displayed as follows —

The string after calling the capitalize() function: Hii! welcome to tutorialspoint. The origial string is: hii! welcome to tutorialspoint.

Источник

How to Capitalize a String in Python: Upper(), Capitalize(), And More

How to Capitalize a String in Python Featured Image

Today, we’ll be looking at how to capitalize a string in Python. There are a few built-in functions for this problem, but we can also roll our own solution. In short, the `capitalize()`python method exists for this purpose. That said, if you need something a bit different than what this method provides (e.g. only capitalizing the first letter), then you might need to roll your own solution. That said, if you’re looking for a bit more of a description, keep reading.

Table of Contents

Video Summary

Opens in a new tab.

With that said, let’s move on to the challenge.

Challenge

For today’s challenge, I had a really fun idea that I might turn into a series. Given how different the `capitalize()`python method is from our solutions, I wonder how hard it would be to duplicate some of the behavior. For example, could you write your own version of the capitalize method that follows the method description?

Opens in a new tab.

Return a copy of the string with its first character capitalized and the rest lowercased. Source: Python Documentation

Opens in a new tab.

If you’d like to share a solution, feel free to use #RenegadePython on Twitter, and I’ll give it a share!

A Little Recap

# A capitalize function leveraging character values def capitalize_ascii(string): character = string[0] if 97 

Likewise, here are a few python resources from the folks at Amazon (#ad):

  • Effective Python: 90 Specific Ways to Write Better PythonOpens in a new tab.
  • Python Tricks: A Buffet of Awesome Python FeaturesOpens in a new tab.
  • Python Programming: An Introduction to Computer ScienceOpens in a new tab.

Once again, thanks for stopping by. I hope this article was helpful!

How to Python (42 Articles)—Series Navigation

The How to Python tutorial series strays from the usual in-depth coding articles by exploring byte-sized problems in Python. In this series, students will dive into unique topics such as How to Invert a Dictionary, How to Sum Elements of Two Lists, and How to Check if a File Exists.

Each problem is explored from the naive approach to the ideal solution. Occasionally, there’ll be some just-for-fun solutions too. At the end of every article, you’ll find a recap full of code snippets for your own use. Don’t be afraid to take what you need!

Opens in a new tab.

If you’re not sure where to start, I recommend checking out our list of Python Code Snippets for Everyday Problems. In addition, you can find some of the snippets in a Jupyter notebook format on GitHub,

If you have a problem of your own, feel free to ask. Someone else probably has the same problem. Enjoy How to Python!

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Latest Code Posts

Best practices often focus on minor difference in programming approaches, but what about the more fundamental ones? Surely, beginners care about those.

Poetry was life changing for me as a Python developer. You really ought to try it.

About Me

Welcome to The Renegade Coder, a coding curriculum website run by myself, Jeremy Grifski. If you like what you see, consider subscribing to my newsletter. Right now, new subscribers will receive a copy of my Python 3 Beginner Cheat Sheet. If newsletters aren't your thing, there are at least 4 other ways you can help grow The Renegade Coder. I appreciate the support!

The Renegade Coder is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com.

Longest Active Series

Источник

Python capitalize String method | First, all or every Letter example

Python capitalizes string method is used to converts the first character of a string (first letter of the first word in the sentence) to the capital (uppercase) letter. The string will same if the string has the first character capital.

Python capitalize String method First, all or every Letter example

Syntax

The capitalize() method returns a string where the first letter is upper case. And this method does not take any parameter.

Python capitalize the first letter of a sentence

This is a simple example of how to use the capitalize() method in python. The output should be uppercase of the first letter.

txt_str = "hello, and welcome to my world." x = txt_str.capitalize() print(x)

Output: Hello, and welcome to my world.

Interview Questions & Answer

Q: How to Python capitalize all letters?

Answer: To change a string into uppercase in use upper() method in python.

txt_str = "hello, and welcome to my world." x = txt_str.upper() print(x)

Q: How to Python capitalize the first letter of every word in a string?

Answer: To capitalize the first letter of each word in a string use title() method in python.

txt_str = "hello, and welcome to my world." x = txt_str.title() print(x)

Q: What happens if the first character is a number?

Answer: If in a sentence first, character (letter) is is number then nothing will change in a sentence. Try out yourself with Non-alphabetic First Character and do comment.

txt_str = "7 hello, and welcome to my world." x = txt_str.capitalize() print(x)

Output: 7 hello, and welcome to my world.

Do comment if you have any doubts and suggestions on the tutorial.

Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6
Python 3.7
All Examples of Python capitalize are in Python 3, so it may change its different from python 2 or upgraded versions.

Источник

Читайте также:  Название документа
Оцените статью