Python: Create a coded string from a given string, using specified formula
Python Basic - 1: Exercise-121 with Solution
Write a Python program to create a coded string from a given string, using a specified formula.
Replace all 'P' with '9', 'T' with '0', 'S' with '1', 'H' with '6' and 'A' with '8'
Sample Solution-1:
Python Code:
# Define a function 'test' that translates characters in the input string based on a mapping.
def test(input_str):
# Use the 'translate' method along with 'str.maketrans' to create a mapping and perform translation.
translated_str = input_str.translate(str.maketrans('PTSHA', '90168'))
return translated_str
# Test the 'test' function with different input strings.
str1 = "PHP"
print("Original string: ", str1)
print("Coded string: ", test(str1))
str2 = "JAVASCRIPT"
print("\nOriginal string: ", str2)
print("Coded string: ", test(str2))
Sample Output:
Original string: PHP Coded string: 969 Original string: JAVASCRIPT Coded string: J8V81CRI90
Explanation:
Here is a breakdown of the above Python code:
- Translation function (test function):
- The "test()" function is defined to perform character translation in an input string.
- It uses the str.translate method along with str.maketrans to create a mapping and perform the translation.
Flowchart:
Sample Solution-2:
Python Code:
# Define a function 'test' that replaces specific characters in the input string with predefined values.
def test(input_str):
# Use the 'replace' method to replace each specified character with its corresponding value.
translated_str = input_str.replace('P', '9').replace('T', '0').replace('S', '1').replace('H', '6').replace('A', '8')
return translated_str
# Test the 'test' function with different input strings.
str1 = "PHP"
print("Original string: ", str1)
print("Coded string: ", test(str1))
str2 = "JAVASCRIPT"
print("\nOriginal string: ", str2)
print("Coded string: ", test(str2))
Sample Output:
Original string: PHP Coded string: 969 Original string: JAVASCRIPT Coded string: J8V81CRI90
Explanation:
Here is a breakdown of the above Python code:
- Translation Function (test function):
- The "test()" function is defined to replace specific characters in an input string with predefined values.
- It uses the "replace" method to replace each occurrence of the specified characters with their corresponding values.
Flowchart:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to calculate Euclid's totient function of a given integer. Use a primitive method to calculate Euclid's totient function.
Next: Write a Python program to check if a given string contains only lowercase or uppercase characters.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/basic/python-basic-1-exercise-121.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics