[SOLVED] Python 3: String Formatting Issues.

ExtreamChaozZ

Distinguished
Dec 24, 2014
229
3
18,715
Hiya,

I have run into an issue where i can't format a string to be printed.
The function is suppossed to convert Binary into Text which is does brilliantly but the printed out result is formatted all the way the right and not the left.
I have tried resolving this by looking up how to format the string but im getting no luck. Im hoping someone can resolve this issue for me.

Heres the code: https://pastebin.pl/view/9bbd4aa9

I tried different ways to format it but im still new to Python 3. I tried putting "ascii_text" into another string to format it, so i could print that string but it didn't work.
The method i tried is also on the pastebin link.

Some advice for this would be great.
Heres a quick Binary code that can be used: "0100100001100101011011000110110001101111"
The decoded version should say "Hello".
 
Solution
Okay then.

You know that \n has the effect of adding a new line. Which is expected behavior.

"Hello" does appear to be on a new line below "Your message in Text" but clearly moved to the right.

34 leading spaces with a trailing space after Hello as near as I can tell via some monitor "measurements".

Or some number of "tabs" perhaps.

Walk through your function by adding some additional/temporary print commands to show:

binary_int
byte_number
binary_array

print each to observe the values and appearance.

Another way to delve into it all is to input just the binary for H. Determine the print location.

Then add e, the first l, the second l, and lastly o.

Where does H appear

Where does He appear

Where does Hel appear...

ExtreamChaozZ

Distinguished
Dec 24, 2014
229
3
18,715
@Ralston18 It shouldn't be "olleH". If you remove the "clear()" call and the the top elif function. Not sure how it reversed it for you. I just double checked the code and it prints out fine apart from the format issue i mentioned.

I isolated the function and removed the "clear()" call. Does that show the problem better now?
https://pastebin.pl/view/8d2459dc

Im using Visual Studio Code, i dont see if thats causing any problems though.

Here's an image of my function block and the printed result.
View: https://imgur.com/a/pPjXCwc
 
Last edited:

Ralston18

Titan
Moderator
Seeing the printed results helped. I misunderstood the formatting problem.

The printed example shows "Hello" over to the right and the requirement is that "Hello" should be just below "Your message in text" - correct?

New line.

Noted the use of "\n" in your print statements.

However you did not use/apply that in your print(ascii_text) line.

Experiment a bit with the use of \n in your last two lines of code.

Any other similar controls for print statements?
 

ExtreamChaozZ

Distinguished
Dec 24, 2014
229
3
18,715
@Ralston18 The entire section is suppossed to print as follows:
Your Message in Binary:
0100100001100101011011000110110001101111

Your Message in Text:
Hello

By adding another "\n" doesn't help as all that does is push the print line down 1 line. All that needs happening is moving the "Hello" to the far left as show above. Issue is i know ruffly the syntax for formatting strings but i cant get it to work, no matter where i place it in the function.

My only other option i can think of is to redo the whole funcition in different syntax to find a work around.
 

Ralston18

Titan
Moderator
Okay then.

You know that \n has the effect of adding a new line. Which is expected behavior.

"Hello" does appear to be on a new line below "Your message in Text" but clearly moved to the right.

34 leading spaces with a trailing space after Hello as near as I can tell via some monitor "measurements".

Or some number of "tabs" perhaps.

Walk through your function by adding some additional/temporary print commands to show:

binary_int
byte_number
binary_array

print each to observe the values and appearance.

Another way to delve into it all is to input just the binary for H. Determine the print location.

Then add e, the first l, the second l, and lastly o.

Where does H appear

Where does He appear

Where does Hel appear

And so on.

Any pattern?
 
Solution

ExtreamChaozZ

Distinguished
Dec 24, 2014
229
3
18,715
@Ralston18 Brilliant. The problem was the "binary_array" It was printing out invisible numbers. By adding [34:] at the end of that string i was able to remove them from the print.

Heres the printed results for a before and after comparison:
View: https://imgur.com/a/W25G1FZ


Didn't even think to try printing the other variables. It really is the simplist answer sometimes huh.

Cheers for your help mate.