Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing individual letters in a list

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])
like image 960
Josh Tam Avatar asked Nov 29 '25 13:11

Josh Tam


2 Answers

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
like image 129
Tim Biegeleisen Avatar answered Dec 01 '25 01:12

Tim Biegeleisen


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)
like image 27
playerJX1 Avatar answered Dec 01 '25 01:12

playerJX1