top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Acronym Generator in Python

In this tutorial, you'll learn to create a simple Acronym Generator using Python.


What is an acronym?

An acronym is a word formed from abbreviating a phrase by combining certain letters of the phrase (often the first letter of each word) into a single word.


Note: Prepositions like 'of' are usually not considered for acronyms.


How an acronym generator works?

An Acronym Generator will take a String as an input and for the output it will return the initials of all the words in the String.




 

How to create acronym using python?


Step 1: Get the user input.

First of all, we need to get the phrase from the user.

We can do it easily with the use of input() method, the user input will be stored in an input_user variable.


#adding the user input
input_user = input("Please Type a phrase: ")

Step 2: Ignore prepositions.

In the second step, we have to ignore ‘of’ from the user input .

#ignore 'of'
clean_input = (input_user.replace('of', ''))

Step 3: Create a list of words.

Next step, we need to extract the words from the clean_input string and to store them as a list in phrase variable.

# Split words
phrase = clean_input.split()

Step 4: Code the acronym generator's logic.

Then, we initilize an empty variable acronym .

acronym = ""

we are going to extract the first letter of every word stored in phrase using slicing operator and adding it to acronym variable than capitalizing it with the use of upper() function. .

for w in phrase:
    acronym = acronym + w[0].upper()

Finally, Let's add a print statement

print('The acronyme of ' + input_user + ' is '+ acronym)

Step 5: Run the code

let's try running our Code!




 

Source Code

Thank you so much for reading! I hope you found this project useful.

You can find the complete source code of this project here ==> SourceCode


1 comment

Recent Posts

See All

1 則留言


Madison Wethington
Madison Wethington
2022年5月14日

Thank you so much for making this. I'm taking an online course on python and the comprehension questions have a horrible hint system coupled with being new I can't tease apart how to make python happy when it throws errors.. This script you made helped break down what each step is and how each function works with each other. Mine ended up looking a little different, but little things like giving the variable the string index iteration BEFORE the string modifier ".upper" was a big sticking point for me and the error "'builtin_function_or_method' object is not subscriptable" was not helpful in telling me what I did wrong, just that I was wrong. Again, thank you so so much for putting…

按讚
bottom of page