Python uppercase one letter

Python: Capitalize the first letter of each word in a string?

In this article we will discuss 5 different ways to convert first letter of each word in a string to uppercase. We will also discuss what are the limitations of each approach and which one is best for us.

Use title() to capitalize the first letter of each word in a string in python

Python Str class provides a member function title() which makes each word title cased in string. It means, it converts the first character of each word to upper case and all remaining characters of word to lower case.

Let’s use this to capitalize the first letter of each word in a string,

sample_text = "this is a sample string" # Capitalize the first letter of each word i.e. # Convert the first letter of each word to Upper case and all other to lower case result = sample_text.title() print(result)

Frequently Asked:

It worked fine with this solution, but there is a caveat. The title() function not only capitalize the first letter of each word in a string but also makes all remaining characters of each word to upper case. For example,

sample_text = "33a. it's GONE too far" # Capitalize the first letter of each word result = sample_text.title() print(result)

There are 3 unexpected behaviors in above example,

  • In this example it converted “GONE” to “Gone”, because for each word in string it makes only first character as upper case and all remaining characters as lower case.
  • It converted “it’s” to “It’S” , because it considered “it’s” as two separate words.
  • It converted “33a” to “33A” , because it considered “a” as the first letter of word ’33a’.
Читайте также:  Javascript writing in iframe

So, title() function is not the best solution for capitalizing the first letter of each word in a string. Let’s discuss an another solution,

Use capitalize() to capitalize the first letter of each word in a string

Python’s Str class provides a function capitalize(), it converts the first character of string to upper case. Where as it is already in upper case then it does nothing.

We can use this capitalize() to capitalize the first letter of each word in a string. For that, we need to split our string to a list of words and then on each word in the list we need to call the capitalize() function. Then we need to join all the capitalized words to form a big string.

Let’s understand this with an example,

def capitalize_each_word(original_str): result = "" # Split the string and get all words in a list list_of_words = original_str.split() # Iterate over all elements in list for elem in list_of_words: # capitalize first letter of each word and add to a string if len(result) > 0: result = result + " " + elem.strip().capitalize() else: result = elem.capitalize() # If result is still empty then return original string else returned capitalized. if not result: return original_str else: return result sample_text = "33a. it's GONE too far" result = capitalize_each_word(sample_text) print(result)

It converted the first letter of each word in string to upper case.

Instead of writing the big function, we can achieve same using generator expressions i.e.

sample_text = "33a. it's GONE too far" result = ' '.join(elem.capitalize() for elem in sample_text.split()) print(result)

Here we split the string to words and iterated our each word in string using generator expression. While iterating, we called the capitalized() function on each word, to convert the first letter to uppercase and the joined that word to a string using ‘ ‘ as delimiter.

It served the purpose, but there can be one issue in this approach i.e. if words in original string are separated by more than one white spaces or tabs etc. Then this approach can cause error, because we are joining all capitalized words using same delimiter i.e. a single white space. Checkout this example,

sample_text = "this is a sample string" result = ' '.join(elem.capitalize() for elem in sample_text.split()) print(result)

Here original string had multiple spaces between words, but in our final string all capitalized words are separated by a single white space. For some this might not be the correct behavior. So, to rectify this problem checkout our next approach.

Using string.capwords() to capitalize the first letter of each word in a string

Python’s string module provides a function capwords() to convert the first letter to uppercase and all other remaining letters to lower case.
It basically splits the string to words and after capitalizing each word, joins them back using a given seperator. Checkout this example,

import string sample_text = "it's gone tOO far" result = string.capwords(sample_text) print(result)

Problem with is solution is that it not only converts the first letter of word to uppercase but also makes the remaining letters of word to lower case. For some, this might not be the correct solution.

So, let’s discuss our final and best solution that does what’s only expected from it.

Using Regex to capitalize the first letter of each word in a string

Using regex, we will look for the starting character of each word and the convert to uppercase. For example,

import re def convert_to_uupercase(m): """Convert the second group to uppercase and join both group 1 & group 2""" return m.group(1) + m.group(2).upper() sample_text = "it's gone tOO far" result = re.sub("(^|\s)(\S)", convert_to_uupercase, sample_text) print(result)

It capitalized only first character of each word in string and do not modifies the whitespaces between words.

How did it worked ?

We created use a pattern “(^|\s)(\S)”. It looks for string patterns that starts with zero or more whitespaces and then has a non whitespace character after that. Then for each matching instance, it grouped both initial whitespaces and the first character as separate groups. Using regex.sub() function, we passed each matching instance of the pattern to a function convert_to_uppercase(), which converts the second group i.e. first letter of word to upper case and then joins it with the first group(zero or more white spaces).

sample_text = "it's gone tOO far"

The function convert_to_uupercase() was called 4 times by regex.sub() and in each call group 1 & 2 of match object were,

'' and 'i' ' ' and 'g' ' ' and 't' ' ' and 'f'

Inside convert_to_uupercase(), it converted the second group i.e. first character of each word to uppercase.

So, this is how we can capitalize the first letter of each word in a string using regex and without affecting any other character of the string.

The complete example is as follows,

import string import re def capitalize_each_word(original_str): result = "" # Split the string and get all words in a list list_of_words = original_str.split() # Iterate over all elements in list for elem in list_of_words: # capitalize first letter of each word and add to a string if len(result) > 0: result = result + " " + elem.strip().capitalize() else: result = elem.capitalize() # If result is still empty then return original string else returned capitalized. if not result: return original_str else: return result def main(): print('*** capitalize the first letter of each word in a string ***') print('*** Use title() to capitalize the first letter of each word in a string ***') print('Example 1:') sample_text = "this is a sample string" # Capitalize the first letter of each word i.e. # Convert the first letter of each word to Upper case and all other to lower case result = sample_text.title() print(result) print('Example 2:') sample_text = "33a. it's GONE too far" # Capitalize the first letter of each word result = sample_text.title() print(result) print('*** Use capitalize() to capitalize the first letter of each word in a string ***') sample_text = "33a. it's GONE too far" result = capitalize_each_word(sample_text) print(result) print('Using capitalize() and generator expression') result = ' '.join(elem.capitalize() for elem in sample_text.split()) print(result) print('Example 2:') sample_text = "this is a sample string" result = ' '.join(elem.capitalize() for elem in sample_text.split()) print(result) print('*** Using string.capwords() to capitalize the first letter of each word in a string ***') sample_text = "it's gone tOO far" result = string.capwords(sample_text) print(result) print('*** Using Regex to capitalize the first letter of each word in a string ***') sample_text = "it's gone tOO far" result = re.sub("(^|\s)(\S)", convert_to_uupercase, sample_text) print(result) def convert_to_uupercase(m): """Convert the second group to uppercase and join both group 1 & group 2""" return m.group(1) + m.group(2).upper() if __name__ == '__main__': main()
*** capitalize the first letter of each word in a string *** *** Use title() to capitalize the first letter of each word in a string *** Example 1: This Is A Sample String Example 2: 33A. It'S Gone Too Far *** Use capitalize() to capitalize the first letter of each word in a string *** 33a. It's Gone Too Far Using capitalize() and generator expression 33a. It's Gone Too Far Example 2: This Is A Sample String *** Using string.capwords() to capitalize the first letter of each word in a string *** It's Gone Too Far *** Using Regex to capitalize the first letter of each word in a string *** It's Gone TOO Far

Источник

8 Ways to Capitalize First Letter in Python

8 Ways to Capitalize First Letter in Python

Strings are one of the most used python data structures. While programming in python, some strings are used in uppercase while some are in lowercase and some in combination. Hence, it is chaotic to pay attention to every string you create and use while programming, and also quite difficult to correct it manually.

As it is important and default format to capitalize the first letter of every word for readers convenience, we have presented a detailed article representing 8 different methods to capitalize the first letter in python and the examples. But, before learning those methods, let us have a brief introduction to strings in python.

What are Strings in Python?

Python string is the collection of the characters surrounded by single quotes, double quotes, or triple quotes. The computer does not understand the characters; internally, it stores manipulated characters as the combination of the 0’s and 1’s. This means that strings can be parsed into individual characters and that individual characters can be manipulated in various ways.

This is what makes Python so versatile: you can do almost anything with it, and some things even work the way they do in another language. To learn more about strings in python, refer to our article «4 Ways to Convert List to String in Python».

For Example

Источник

Оцените статью