Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print: Tips and tricks
#1
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 .
Reply
#2
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)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Try
print(''.join(chr(9812 + x) for x in range(12)))
Reply
#4
(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() 
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
for word in 'f-strings are awesome'.split():
    print(f'{word.capitalize():\N{smiling face with sunglasses}^15}')
[Image: ehIJBe.jpg]
Reply
#6
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 ?
Reply
#7
(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()
Reply
#8
(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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
(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.
Reply
#10
(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)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner/Intermediate Tips Ex_database 1 2,015 Sep-27-2019, 04:24 PM
Last Post: Larz60+
  Quick tips and tricks for python juliopa 2 28,785 Jul-03-2019, 09:41 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020