Home > other >  These two strings are supposed to be the same length but when printed they do not appear that way
These two strings are supposed to be the same length but when printed they do not appear that way

Time:11-25

a="|:watch:️ :mobile phone: :mobile phone with arrow: :laptop: :keyboard: :desktop computer:        |"
b="|:printer: :computer mouse: :trackball: :joystick: :clamp: :computer disk: :floppy disk: :optical|"

both of these strings should be 98 characters, but when printing with a monospaced font (in my terminal) it shows b as being longer

terminal output when printing length of each string followed by string

This shouldn't be an issue but I'm trying to draw a box around some text and this glitch causes the table to be misaligned. Is it possible that the font is not properly monospaced? I am using VS Code for the Web.

Thank you kindly for your time.

CodePudding user response:

I was guessing that one of the chars in a is strange. We can see this if we print them:

In [4]: for i, char in enumerate(a):
   ...:     print((i, char))
   ...:
(0, '|')
(1, ':')
(2, 'w')
(3, 'a')
(4, 't')
(5, 'c')
(6, 'h')
(7, ':')
(8, '️')
(9, ' ')
(10, ':')
(11, 'm')
(12, 'o')
(13, 'b')
(14, 'i')
(15, 'l')
(16, 'e')
(17, ' ')
(18, 'p')
(19, 'h')
(20, 'o')
(21, 'n')
(22, 'e')
(23, ':')
(24, ' ')
(25, ':')
(26, 'm')
(27, 'o')
(28, 'b')
(29, 'i')
(30, 'l')
(31, 'e')
(32, ' ')
(33, 'p')
(34, 'h')
(35, 'o')
(36, 'n')
(37, 'e')
(38, ' ')
(39, 'w')
(40, 'i')
(41, 't')
(42, 'h')
(43, ' ')
(44, 'a')
(45, 'r')
(46, 'r')
(47, 'o')
(48, 'w')
(49, ':')
(50, ' ')
(51, ':')
(52, 'l')
(53, 'a')
(54, 'p')
(55, 't')
(56, 'o')
(57, 'p')
(58, ':')
(59, ' ')
(60, ':')
(61, 'k')
(62, 'e')
(63, 'y')
(64, 'b')
(65, 'o')
(66, 'a')
(67, 'r')
(68, 'd')
(69, ':')
(70, ' ')
(71, ':')
(72, 'd')
(73, 'e')
(74, 's')
(75, 'k')
(76, 't')
(77, 'o')
(78, 'p')
(79, ' ')
(80, 'c')
(81, 'o')
(82, 'm')
(83, 'p')
(84, 'u')
(85, 't')
(86, 'e')
(87, 'r')
(88, ':')
(89, ' ')
(90, ' ')
(91, ' ')
(92, ' ')
(93, ' ')
(94, ' ')
(95, ' ')
(96, ' ')
(97, '|')

So we can see that at index 8 there is some kind of "zero width" character.

We can inspect the char to find its codepoint:

In [5]: ord(a[8])
Out[5]: 65039

This appears to be Unicode "VARIATION SELECTOR-16": https://www.fileformat.info/info/unicode/char/fe0f/index.htm

Some kind of modifying character that has no printable representation of its own.

CodePudding user response:

try to add one profit to a or copy this and try...

a="|:watch:️ :mobile phone: :mobile phone with arrow: :laptop: :keyboard: :desktop computer:         |"
b="|:printer: :computer mouse: :trackball: :joystick: :clamp: :computer disk: :floppy disk: :optical|"
  • Related