9

I tried to print the letter "A" using patterns in python

def printA(length,height,symbol):
    a = [[" " for i in range(length)] for i in range(height)]
    for i in range(height):
        for j in range(length):
            if j == 0 or i == 0 or i == height // 2 or j == length - 1:a[i][j] = symbol
    return a

It works for normal characters like *,/+,-,#,$,% .. etc.,

Output: Normal Characters

#######
#     #
#     #
#######
#     #
#     #
#     #

Emoji


     
     

     
     
     

if i can find the length of the emoji , then i will be able to change the spaces to the length of the emoji so this problem won't occur, is there any way to do this

Note : The above code works only for characters and not strings

EDIT :
As of snakecharmerb's answer it works for printing just the character A but when i try to print sequnces of A i.e more than once it just misplacing the emojis

Example : I tried to print AAAAA

Output :

From the above output as we increase the letter's position it gets repositioning itself is there any way to prevent this from occuring

I printed the AAAAA like this

a = printA(7,7,"")
for i in a:
    for k in range(5):print(*(i),end="  ")
    print()
Coder
  • 91
  • 6
  • see if these two responses give you what you want. https://stackoverflow.com/questions/43146528/how-to-extract-all-the-emojis-from-text. https://stackoverflow.com/questions/36216665/find-there-is-an-emoji-in-a-string-in-python3. You should be able to use len("".encode("utf-8")) to check for length – Joe Ferndz Aug 17 '20 at 07:05
  • I dont think your code has a problem only with emojis. Try to run the code using `printA(5,6,"##")` and you will find that it prints similar to the emojis. You need to fix the code differently. – Joe Ferndz Aug 17 '20 at 07:34
  • 2
    @JoeFerndz I have mentioned characters and not strings the symbol must be a character and not combination of characters – Coder Aug 18 '20 at 16:56

2 Answers2

10

What matters is the width of the glyph in the font that displays the character, rather then the length of the character as a Python string. This information isn't available to Python but we can guess based on whether the symbol is a wide East Asian character, as defined by the unicode standard as reported by the unicodedata module.

import unicodedata 


def printA(length, height, symbol):  
    # Two spaces for "wide" characters, one space for others.
    spacer = '  ' if unicodedata.east_asian_width(symbol) == 'W' else ' ' 
    a = [[spacer for i in range(length)] for i in range(height)]  
    for i in range(height): 
        for j in range(length): 
            if j == 0 or i == 0 or i == height // 2 or j == length - 1:
                a[i][j] = symbol  
    return a

Using the East Asian Width property works because

emoji characters were first developed through the use of extensions of legacy East Asian encodings, such as Shift-JIS, and in such a context they were treated as wide characters. While these extensions have been added to Unicode or mapped to standardized variation sequences, their treatment as wide characters has been retained, and extended for consistency with emoji characters that lack a legacy encoding.

(link)

You may also need to check that your terminal uses a monospaced font.

See also this Q&A for some issues around aligning text with varying character width properties in a terminal, as well as this and this.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
-1

You can use the following to check for length.

len("".encode("utf-8"))

More information can be found in these two stack overflow responses.

Find there is an emoji in a string in python3

How to extract all the emojis from text?

Instead of figuring out the length of the emoji, can't you do this?

a = [[" "*len(symbol) for i in range(length)] for i in range(height)]
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33