Printing individual letters in a list

1.6k Views Asked by At

I am writing a program that takes a statement or phrase from a user and converts it to an acronym.

It should look like:

Enter statement here:
> Thank god it's Friday
Acronym : TGIF

The best way I have found to accomplish this is through a list and using .split() to separate each word into its own string and am able to isolate the first letter of the first item, however when I try to modify the program for the following items by changing to print statement to:

print("Acronym :", x[0:][0])

it just ends up printing the entirety of the letters in the first item.

Here's what I have gotten so far, however it only prints the first letter of the first item...

acroPhrase = str(input("Enter a sentence or phrase : "))     
acroPhrase = acroPhrase.upper()  

x = acroPhrase.split(" ")  
    print("Acronym :", x[0][0])
3

There are 3 best solutions below

0
On

Using re.sub with a callback we can try:

inp = "Peas porridge hot"
output = re.sub(r'(\S)\S*', lambda m: m.group(1).upper(), inp)
print(output)  # PPH
1
On

The code needs to iterate through the .split result. For example, using a list comprehension:

inp = "Thank god its friday"
inp = inp.split()
first_lets = [word[0] for word in inp]
1
On
acroPhrase = str(input("Enter a sentence or phrase : "))     
acroPhrase = acroPhrase.upper()  

x = acroPhrase.split(" ")  
result = ''
for i in x:
    word = list(i)
    result+=word[0]

print(result)