Write the output of the following code
# !/usr/bin/python
from string import maketrAns. # Required to call maketrAns. function.
intab = "aeiou" outtab = "12345"
trantab = maketrAns.(intab, outtab) str = "this is string example….wow!!!" print str.trAns.late(trantab, ‘xm’)
Answers
Explanation:
The following example shows the usage of maketrans() method. Under this, every vowel in a string is replaced by its vowel position −
Live Demo
#!/usr/bin/python
from string import maketrans # Required to call maketrans function.
intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)
str = "this is string example....wow!!!"
print str.translate(trantab)
When we run above program, it produces following result −
th3s 3s str3ng 2x1mpl2....w4w!!!
Output :
th3s 3s str3ng 2x1mpl2....w4w!!!
If, last statement is str.translate(trantab).
TypeError: translate() takes exactly one argument (2 given)
If it is str.translate(trantab,'xm')
We think that you made a mistake with last statement. So, here we are providing the outputs for both codes.
Program 1:
intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)
str = "this is string example....wow!!!"
print (str.translate(trantab))
Output:
th3s 3s str3ng 2x1mpl2....w4w!!!
Program 2:
intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)
str = "this is string example....wow!!!"
print (str.translate(trantab,'xm'))
Output: (Error)
Traceback (most recent call last):
File "main.py", line 9, in <module>
print (str.translate(trantab,'xm'))
TypeError: translate() takes exactly one argument (2 given)
Explanation :
- The String class's method maketrans() takes exactly two strings as arguments.
- The first argument's each character is mapped to the corresponding indexed character of the second string, by the function maketrans().
- So, the length of both the strings should be same.
- The mapped values are stored into the variable as a dictionary.
- Then, take a string as an input, in which you want to make translations to the characters based on the dict variable.
- Now, upon the string taken, apply translate() passing the dict variable as a parameter.
- translate() takes only one argument, that too of a maketrans() mapped variable as the one.
- This function replaces the key values of the dictionaries present in the statement, with their values. As in, 'a' with '1', 'e' with '2', 'i' with '3', 'o' with '4', 'u' with '5'. That's it!
Learn more :
- Advantages of Programming in python
brainly.in/question/11007952
- Indentation is must in python. Know more about it at :
brainly.in/question/17731168