简体   繁体   中英

how to print unicode number series in python?

I am just trying to print the Unicode number ranging from 1 to 100 in python. I have searched a lot in StackOverflow but no question answers my queries.

So basically I want to print Bengali numbers from ১ to ১০০. The corresponding English number is 1 to 100.

What I have tried is to get the Unicode number of ১ which is '\১'. Then I have tried to increase this number by 1 as depicted in the following code:

x = '\u09E7'
print(x+1)

But the above code says to me the following output.

TypeError: can only concatenate str (not "int") to str

So what I want is to get a number series as following:

১, ২, ৩, ৪, ৫, ৬, ৭, ৮, ৯, ১০, ১১, ১২, ১৩, ............, ১০০

TypeError: can only concatenate str (not "int") to str1 I wish if there is any solution to this. Thank you.

Make a translation table. The function str.maketrans() takes a string of characters and a string of replacements and builds a translation dictionary of Unicode ordinals to Unicode ordinals. Then, convert a counter variable to a string and use the translate() function on the result to convert the string:

#coding:utf8
xlat = str.maketrans('0123456789','০১২৩৪৫৬৭৮৯')
for i in range(1,101):
    print(f'{i:3d} {str(i).translate(xlat)}',end=' ')

Output:

1 ১ 2 ২ 3 ৩ 4 ৪ 5 ৫ 6 ৬ 7 ৭ 8 ৮ 9 ৯ 10 ১০ 11 ১১ 12 ১২ 13 ১৩ 14 ১৪ 15 ১৫ 16 ১৬ 17 ১৭ 18 ১৮ 19 ১৯ 20 ২০ 21 ২১ 22 ২২ 23 ২৩ 24 ২৪ 25 ২৫ 26 ২৬ 27 ২৭ 28 ২৮ 29 ২৯ 30 ৩০ 31 ৩১ 32 ৩২ 33 ৩৩ 34 ৩৪ 35 ৩৫ 36 ৩৬ 37 ৩৭ 38 ৩৮ 39 ৩৯ 40 ৪০ 41 ৪১ 42 ৪২ 43 ৪৩ 44 ৪৪ 45 ৪৫ 46 ৪৬ 47 ৪৭ 48 ৪৮ 49 ৪৯ 50 ৫০ 51 ৫১ 52 ৫২ 53 ৫৩ 54 ৫৪ 55 ৫৫ 56 ৫৬ 57 ৫৭ 58 ৫৮ 59 ৫৯ 60 ৬০ 61 ৬১ 62 ৬২ 63 ৬৩ 64 ৬৪ 65 ৬৫ 66 ৬৬ 67 ৬৭ 68 ৬৮ 69 ৬৯ 70 ৭০ 71 ৭১ 72 ৭২ 73 ৭৩ 74 ৭৪ 75 ৭৫ 76 ৭৬ 77 ৭৭ 78 ৭৮ 79 ৭৯ 80 ৮০ 81 ৮১ 82 ৮২ 83 ৮৩ 84 ৮৪ 85 ৮৫ 86 ৮৬ 87 ৮৭ 88 ৮৮ 89 ৮৯ 90 ৯০ 91 ৯১ 92 ৯২ 93 ৯৩ 94 ৯৪ 95 ৯৫ 96 ৯৬ 97 ৯৭ 98 ৯৮ 99 ৯৯ 100 ১০০

You can try this. Convert the character to an integer . Do the addition and the convert it to character again. If the number is bigger than 10 you have to convert both digits to characters that's why we are using modulo % .

if num < 10:
   x = ord('\u09E6')
   print(chr(x+num))
elif num < 100:
   mod = num % 10
   num = int((num -mod) / 10)
   x = ord('\u09E6')
   print(''.join([chr(x+num), chr(x+mod)]))
else:
   x = ord('\u09E6')
   print(''.join([chr(x+1), '\u09E6', '\u09E6']))

You can try running it here https://repl.it/repls/GloomyBewitchedMultitasking

EDIT: Providing also javascript code as asked in comments.

function getAsciiNum(num){
    zero = "০".charCodeAt(0)
    if (num < 10){
        return(String.fromCharCode(zero+num))
    }
    else if (num < 100) {
      mod = num % 10
      num = Math.floor((num -mod) / 10)
      return(String.fromCharCode(zero+num) + String.fromCharCode(zero+mod))
    }
    else {
      return(String.fromCharCode(zero+1) + "০০")
    }
}

console.log(getAsciiNum(88))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM