python-3.xunicodesubstringhindi

How can I get devnagri substring from a string containing a range of characters in python


Here is my sample string :

string = 'this is a string 3.158 बात करना है'

I want a function that takes this string as argument and returns:
'बात करना है'


Solution

  • The Windows XP alt code for devnagri letters lie between 2309 and 2416. So you can use the following code:

    s = 'this is a string 3.158 बात करना है'
    
    for n,i in enumerate(s):
        if ( 2309 < ord(i) < 2416 ) or (i==' ') and ( 2309 < ord(s[n-1]) <2416 ):
            print(i,end='')