Python Forum
Explaining "translate" method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Explaining "translate" method
#1
Hello,

I am working through some textbook exercises and was given the task:

Write a program that allows a user to type in a phrase and provides the acronym as the output. The acronym should be in uppercase.

I was able to solve it using two different approaches. The first solution works great and was my attempt after deleting everything and coming back to it fresh on the second day.

The second solution works but I stole code from Stack Overflow and don't fully understand it. Specifically, the last line which is using the translate method. Could someone help me digest this method?

Is it somehow looping through the letterString, looking at each character (ord) in the string and returning a 'none' value (i.e. blank) for any of those characters I selected?

Changing the code to add in "bacon" swaps out those characters for bacon.
print(letterString.translate({ord(i): "bacon" for i in " ',"}))
So, where I am struggling is around the syntax
{ord(i): blah blah} 
How does this syntax know to loop through each character in the string?

I know the second solution is ugly but wanted to learn from it anyway. It hasn't 'clicked' so hoping someone can help me think about it another way.

Thanks so much!


#Allows user to enter a string
phrase = str(input("Enter your phrase: "))

#Splits the string by each space (word)
#Note: after using a split, this sets the variable type to LIST!
split_phrase = phrase.split()

#Set an empty string to contain the acronym
acronym = str("")

#Loop through the items in the LIST of split_phrase
#Take the first element, set it as upper case
#Add it into the acronym variable
for i in split_phrase:
    first_letter = i[0].upper()
    acronym = acronym + first_letter

print("acronym is: " + acronym)
#User enters the string
#Split function divides it based on space
user_input = str(input("Please enter your phrase: ")).split()

#Creates a list.
#Loops through each item (word) in the user_input string. For the 0 index (first character) of each word. Assigns to list 
firstList = [x[0] for x in user_input]

#Creates a new list
#For each item in the list, change it to upper and assign it to the variable
secondList = [element.upper() for element in firstList]

#Creates a string from the list
letterString = str(secondList)

print(letterString.translate({ord(i): None for i in " ',"}))
Reply


Messages In This Thread
Explaining "translate" method - by lummers - Jan-11-2020, 07:45 AM
RE: Explaining "translate" method - by perfringo - Jan-11-2020, 08:33 AM
RE: Explaining "translate" method - by Gribouillis - Jan-11-2020, 09:08 AM
RE: Explaining "translate" method - by ndc85430 - Jan-12-2020, 05:33 PM
RE: Explaining "translate" method - by perfringo - Jan-13-2020, 06:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  for loop and if translate from R in Python Joey21 3 4,002 Feb-06-2018, 02:16 PM
Last Post: sparkz_alot
  Need help explaining what the variables do in my code. BurnedChipotle 6 6,278 Jan-30-2017, 10:48 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020