879282. Print ascii value in python
Easy
Print ascii value in python
Summary
The problem requires printing ASCII values for a given input string. The key concept involved is the use of ASCII codes, which represent characters as unique numerical values.
Detailed Explanation
To solve this problem, we can iterate through each character in the input string and print its corresponding ASCII value. We can do this by using Python's built-in ord() function, which returns the Unicode code point for a given character.
Here is the step-by-step breakdown of the solution:
- Initialize an empty string to store the output.
- Iterate through each character in the input string.
- For each character, use the
ord()function to get its ASCII value. - Append the ASCII value as a string to the output string.
- Print the final output string.
Here is the time and space complexity analysis:
- Time complexity: O(n), where n is the length of the input string.
- Space complexity: O(n), where n is the length of the output string.
Optimized Solutions
Python
python
def print_ascii(input_string):
output = ""
for char in input_string:
output += str(ord(char)) + " "
print(output)
# Test the function
input_string = "Hello World"
print_ascii(input_string)
Not available