This question already has answers here:
How to get the ASCII value of a character
(5 answers)
Closed 2 years ago.
How would one write a code to display the conversion of individual letters in a string to it's ASCII equivalent? One example of the output in shell would look like this:
Enter a 3-letter word: Hey
H = 72
e = 101
y = 121
Use built-in ord function
>>> ord('H')
72
Get the input, and print each character plus its ordinal value.
user_input = input("Enter a 3-letter word: ")
for character in user_input:
print(character + " = " + ord(character))
Related
This question already has answers here:
Efficient way to add spaces between characters in a string
(5 answers)
Closed 1 year ago.
So i am making a very simple python program which takes a string as input from user and adds a '-' between every character but the problem is it adds it to last one too but i don't want that...
Here's the code:
string = input("Enter the string to be traversed: ")
for ch in string:
print(ch , end = "-")
Output:
Enter the string to be traversed: helloworld
h-e-l-l-o-w-o-r-l-d-
I don't want it to print '-' after the last character...
This is a common problem and there is a string method to deal with it. Instead of a for loop, join the characters with a dash.
string = input("Enter the string to be traversed: ")
print("-".join(string))
.join will work with anything that iterates strings. In your case, a string does that, character by character. But a list or tuple would work as well.
This question already has answers here:
Why words are shuffled when I insert English words in any Arabic/Urdu/Persian text on Notepad or MS Word?
(8 answers)
How to make the text direction from right to left
(12 answers)
Closed 1 year ago.
I have a number string and a Persian string that I want to concatenate in python (my IDE is Pycharm) and when I do this, right-to-left breaks down.
num = "1200"
body = "ریال"
total = num + " " + body
print(total)
it results:
1200 ریال
but I expect this:
1200 ریال
what can I do?
There is a special standard character named Right-to-left mark.
you can use it with this expression:
u"\u200F"
So you can correct your code this way:
corrected = u"\u200F" + num + " " + body
print(corrected)
that results:
1200 ریال
the string isn't right to left, you're just adding the number first.
try:
total = body + " " + num
This question already has answers here:
replace all characters in a string with asterisks
(4 answers)
Closed 4 years ago.
str0 = input("Enter str: ")
str0 = str0.replace(str0, '_')
print(str0)
I need to replace each one of the characters without using loops or conditions
str0 = input("Enter str: ")
str0 = '_'*len(str0)
print(str0)
Should work. The multiplication puts the right amount of _'s so that all of them are replaced.
This question already has answers here:
How do I split a string into a list of characters?
(15 answers)
Closed 5 years ago.
How would you go about separating each character in a given input and turn it into a list?
For example I have
import string
print ("Enter string")
x = input("")
Enter string
The quick brown
I want the end result to be
['T','h','e',' ','q','u','i','c','k',' ','b','r','o','w','n']
Y'know, to turn every character as a separate string in a list instead of every word as a separate string.
Thanks!
Simply use list(x) where x is the string.
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 7 years ago.
i want to reverse the order of characters in a string
so take strings as arguments
and return s new string with letters of the original string
example - "hello" would return "olleh"
All I have gotten to is:
def reverse(" "):
string = input("Give me a word")
x = ord(string)
print(x)
You can do it like this:
"hello"[::-1]
You can read about extended-slices here