Python Forum
Explaining "translate" method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Explaining "translate" method
#4
(Jan-11-2020, 07:45 AM)lummers Wrote:
print(letterString.translate({ord(i): "bacon" for i in " ',"}))

The argument to translate is a dictionary and is produced by an expression called a dictionary comprehension. Python has comprehensions for other collections too (namely for sets and lists) and a related concept known as generator expressions. I'm giving you the names of these things so you can go and find out more about them.

Did you also try looking up the documentation for the ord and translate functions?
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,000 Feb-06-2018, 02:16 PM
Last Post: sparkz_alot
  Need help explaining what the variables do in my code. BurnedChipotle 6 6,275 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