Try string functions
s = "ABCDEFGHI"
i = 3
j = 5
print(s[i]) .............
print(s[5+3]) ..........
print(s[i:j+1]) ..........
print(s.find("D")) .......
for
c in s
to grab an individual c from the string s. As it is
in for-loop, c will be initialized to a new character each time the body of
loop is executed. In the end, the string s runs out, so c in s
will return a False
and the loop terminated. See this
example of printing each character of a string on a line of its own.for c in hello world:
print(c)
Fill in the blank
The input is a string of numbers, 12345. It is increasing-digit if
the successive number is larger. Write a program to check if an
input string is increasing-digit. ("12321" is not increasing-digit).
n = input("Enter digits : ")
is_increasing = __________
prev = ___________ #
begin with small number
for d in ____________ : # get each character in the
input
if _________________ : # compare the current char
with the previous one
is_increasing = __________
break
prev =
d
# update the previous char
if is_increasing :
print("Yes, this is an increasing-digit number")
else :
print("No, this is not an increasing-digit number")
in_txt = input("Enter digits : ")
out_txt = ""
arabic_number = "0123456789"
thai_number = "๐๑๒๓๔๕๖๗๘๙"
for _____________________ :
k = ______________________
if k < 0 :
out_txt += _________________
else :
out_txt += _________________
print(out_txt)
s.find(
)
to locate an
arabic numeral in arabic_number
. That will give you an
index. Use that index to get
thai_number
. This is an old trick, invented since the early
days of programming (that we use constant string to store information).
The other way is to use the function ord()
and chr()
.
Here is what they are:print(ord(1))
49
print(chr(49))
1
print(ord(๑))
3665
ord(c)
gives the coding of the character c. The
chr(i)
returns the character with the code i. 1111
2222
3333
....
9999
1112
2223
3334
...
10000
ord()
and chr().
I know by
exploring a bit the code of arabic and thai numeral.print(ord(1))
49
print(ord(๑))
3665
# convert input of arabic numeral to thai
inp = input()
out = ""
for c in inp:
acode = ord(c)
tcode = acode + 3616
out += chr(tcode)
print(out)
How I find out ord()
and chr()
functions? I google "how to convert character in python" and on
my computer the first link that comes up is:
Enjoy!