Home > Net >  Printing individual letters in a list
Printing individual letters in a list

Time:03-31

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])

CodePudding user response:

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

CodePudding user response:

Here just split it and iterate through each to get the first letter.

inp = "Thank god its friday"
inp = inp.split()
first_lets = [word[0] for word in inp]

This will result in the result you wanted. Here we use a list comprehension to do the job. Have a nice day, hope this helped!

CodePudding user response:

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)
  • Related