Python Forum
print: Tips and tricks - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: print: Tips and tricks (/thread-17449.html)

Pages: 1 2


print: Tips and tricks - microphone_head - Apr-11-2019

Hi all.

I found that when I run the print command with "\b" I can produce a tick symbol.

Does anyone have any other tips they would like to share about this function?

print("Microphone_Head ", end ="") ; print("is in the house \b")
Output:
Microphone_Head is in the house 
PS. I'm running Python on a RaspberyPi 2B+, and there really is a tick symbol honest Shifty .


RE: print: Tips and tricks - perfringo - Apr-11-2019

Quote:Unless an 'r' or 'R' prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C.

Escape sequence \b meaning is "ASCII Backspace (BS)". Other sequences and their meanings are available in Python documentiation (String and Byte Literals)


RE: print: Tips and tricks - Gribouillis - Apr-11-2019

Try
print(''.join(chr(9812 + x) for x in range(12)))



RE: print: Tips and tricks - perfringo - Apr-11-2019

(Apr-11-2019, 11:58 AM)Gribouillis Wrote: Try
print(''.join(chr(9812 + x) for x in range(12)))

King is smaller than pawn Big Grin

>>> '♔' < '♙'
True

Little animation:

import time 
import sys 
 
animation = '|/-\\' 
for i in range(25): 
    time.sleep(0.1) 
    sys.stdout.write(f"\r{animation[i % len(animation)]}")
    sys.stdout.flush() 



RE: print: Tips and tricks - snippsat - Apr-11-2019

for word in 'f-strings are awesome'.split():
    print(f'{word.capitalize():\N{smiling face with sunglasses}^15}')
[Image: ehIJBe.jpg]


RE: print: Tips and tricks - microphone_head - Apr-12-2019

Quote:
import time 
import sys 
 
animation = '|/-\\' 
for i in range(25): 
    time.sleep(0.1) 
    sys.stdout.write(f"\r{animation[i % len(animation)]}")
    sys.stdout.flush() 

Do I need to import another library?

I've tried running this in Python 2.7.9 and Python 3.4.2 and I cant't get it to work. It reports a syntax error on the following line:
sys.stdout.write(f"\r{animation[i % len(animation)]}")
FYI. I'm running the Python on a Raspberry Pi 2B+.

(Apr-11-2019, 11:24 AM)perfringo Wrote:
Quote:Unless an 'r' or 'R' prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C.
Escape sequence \b meaning is "ASCII Backspace (BS)". Other sequences and their meanings are available in Python documentiation (String and Byte Literals)

Hi perfringo,

I don't understand what you mean. I've had a quick read of the link you gave Confused . I tried the following:

print("MH\rb")
print("MH\Rb")
and got Huh
Output:
MH b MH\Rb
I was toying with the idea of being able to write some chars to the Python shell before deleting it to make a progress bar. Can you give an example Angel ?


RE: print: Tips and tricks - snippsat - Apr-12-2019

(Apr-12-2019, 08:30 AM)microphone_head Wrote: I've tried running this in Python 2.7.9 and Python 3.4.2 and I cant't get it to work. It reports a syntax error on the following line:
Upgrade your python version,f-string was new in Python 3.6.
Like this it will work in older version.
import time
import sys

animation = '|/-\\'
for i in range(25):
    time.sleep(0.1)
    sys.stdout.write("\r{}".format(animation[i % len(animation)]))
    sys.stdout.flush()



RE: print: Tips and tricks - perfringo - Apr-12-2019

(Apr-12-2019, 08:30 AM)microphone_head Wrote: Do I need to import another library?

I've tried running this in Python 2.7.9 and Python 3.4.2 and I cant't get it to work.
FYI. I'm running the Python on a Raspberry Pi 2B+.

This code uses f-strings available from Python >= 3.6. If you replace this with following, it should work:

sys.stdout.write("\r" + animation[i % len(animation)])
ninjad by snippsat Smile


RE: print: Tips and tricks - microphone_head - Apr-12-2019

(Apr-12-2019, 09:34 AM)snippsat Wrote:
(Apr-12-2019, 08:30 AM)microphone_head Wrote: I've tried running this in Python 2.7.9 and Python 3.4.2 and I cant't get it to work. It reports a syntax error on the following line:
Upgrade your python version,f-string was new in Python 3.6. Like this it will work in older version.
import time import sys animation = '|/-\\' for i in range(25): time.sleep(0.1) sys.stdout.write("\r{}".format(animation[i % len(animation)])) sys.stdout.flush()

Ok, it runs but this is the output on my machine:

Output:
| / - \ | / - \ | / - \ | / - \ | / - \ | / - \ |
Something weird is going on. When I copied the output from my shell window it all appeared on one line, like a list of musical notes. But it does run.


RE: print: Tips and tricks - perfringo - Apr-12-2019

(Apr-12-2019, 08:30 AM)microphone_head Wrote: I was toying with the idea of being able to write some chars to the Python shell before deleting it to make a progress bar. Can you give an example Angel ?

Something along those lines (no f-strings, should work right away)?

import sys, time

def status_bar(activity, progres):
    length = 25                         
    block = int(round(length*progres))
    msg = "\r{0}: [{1}] {2}%".format(activity, "#"*block + "-"*(length-block), round(progres*100, 2))
    if progres >= 1: 
        msg += " Ready\r\n"
    sys.stdout.write(msg)
    sys.stdout.flush()



# usage
for i in range(100):
    time.sleep(0.1)
    status_bar('Activity', i/100.0)
status_bar('Activity', 1)