close

How can I get the first letter of each word in a string?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How can I get the first letter of each word in a string in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How can I get the first letter of each word in a string?

  1. How can I get the first letter of each word in a string?

    This works because you get a list containing all the values of the string (by default split() splits white-space), next you initerate through that list (this returns a single character) and then get the value of the first character of the string.

  2. I get the first letter of each word in a string

    This works because you get a list containing all the values of the string (by default split() splits white-space), next you initerate through that list (this returns a single character) and then get the value of the first character of the string.

Method 1

[ s[0] for s in 'Piethon is good'.split() ]

Method 2

How about you just slice the string when printing (or assign a new variable) and remove start = word[0:][0]:

trans = input("enter a sentence ")
trans = trans.lower()
t_list = trans.split()
for word in t_list:
    print(word[0])

This works because you get a list containing all the values of the string (by default split() splits white-space), next you initerate through that list (this returns a single character) and then get the value of the first character of the string.

You could also use trans = input("enter a sentence ").lower().split() rather than redefine trans into a new variable each time.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read