title: How to explain the str.maketrans function in Python 3.6?
body: <p>I am currently participating in an Udacity course that instructs students on programming using Python. One of the projects has students rename photo files (remove any numbers in the name) in a directory in order to have the files arranged alphabetically, after which a secret message will be spelled out. For instance, if a file name is <code>"48athens"</code>, the program seeks to remove the numbers, leaving only <code>"athens"</code> as the file name.</p>
  
  <p>I am using Python 3.6, while the course instructor is using Python 2.7. I should likely be using Python 2.7 so as to simplify the learning process. However, for now I will keep using Python 3.6.</p>
  
  <p>The way in which the instructor has the files renamed is using the <code>.translate</code> function, which takes two arguments in Python 2.x, while Python 3.x only takes one argument. It removes any numbers (0 through 9) from the file names.</p>
  
  <pre><code>import os
  
  def rename_files(): #Obtain the file names from a folder.
      file_list = os.listdir(r"C:\Users\Dennis\Desktop\OOP\prank\prank")
      print (file_list)
      saved_path = os.getcwd()
      os.chdir(r"C:\Users\Dennis\Desktop\OOP\prank\prank")
      for file_name in file_list: #Rename the files inside of the folder.
          os.rename(file_name, file_name.translate(None, "0123456789"))
      os.chdir(saved_path)
  
  rename_files()
  </code></pre>
  
  <p>However, this does not work in Python 3.x, as it says that:</p>
  
  <pre><code>TypeError: translate() takes exactly one argument (2 given)
  </code></pre>
  
  <p>Thankfully, I found another way using someone's assistance. However, I'm not really sure how it works. Can someone explain the <code>str.maketrans</code> function to me, and what the first two blank arguments in quotes are for? My thought is that it's saying: for the first two characters in the file name, remove any numbers (0 through 9). Is that correct? For instance, in <code>"48athens"</code>, remove the first two characters (4 and 8) if they are numbers between 0 and 9.</p>
  
  <pre><code>import os
  
  def rename_files(): #Obtain the file names from a folder.
      file_list = os.listdir(r"C:\Users\Dennis\Desktop\OOP\prank\prank")
      print (file_list)
      saved_path = os.getcwd()
      os.chdir(r"C:\Users\Dennis\Desktop\OOP\prank\prank")
      for file_name in file_list: #Rename the files inside of the folder.
          os.rename(file_name, file_name.translate(str.maketrans('','','0123456789')))
      os.chdir(saved_path)
  
  rename_files()
  </code></pre>
  
  <p><strong>My Understanding of the Documentation:</strong></p>
  
  <blockquote>
    <p><code>static str.maketrans(x[, y[, z]])</code> 
    This static method returns a translation table usable for <code>str.translate()</code>.</p>
  </blockquote>
  
  <p>It's saying that the arguments passed to <code>str.maketrans</code>, along with the actual function <code>str.maketrans</code>, will make a table that says, "If this character appears, replace it with this character." However, I'm not sure what the brackets are for.</p>
  
  <blockquote>
    <p>If there is only one argument, it must be a dictionary mapping Unicode
    ordinals (integers) or characters (strings of length 1) to Unicode
    ordinals, strings (of arbitrary lengths) or None. Character keys will
    then be converted to ordinals.</p>
  </blockquote>
  
  <p>It's saying that it can only change integers, or characters in strings of length one, to other integers or strings (of any length you want). But I believe I have three arguments, not one.</p>
  
  <blockquote>
    <p>If there are two arguments, they must be strings of equal length, and
    in the resulting dictionary, each character in x will be mapped to the
    character at the same position in y. If there is a third argument, it
    must be a string, whose characters will be mapped to None in the
    result.</p>
  </blockquote>
  
  <p>I have three arguments <code>('', '', '0123456789')</code>. I think <code>x</code> is the first <code>''</code>, and <code>y</code> is the second <code>''</code>. I have the third argument, which is a string <code>'0123456789'</code>, but I don't understand what it means to be mapped to <code>'None'</code>. </p>