Bottom Page

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 .
Quote
#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.
Quote
#3
Try
print(''.join(chr(9812 + x) for x in range(12)))
microphone_head and perfringo like this post
Quote
#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.
Quote
#5
for word in 'f-strings are awesome'.split():
    print(f'{word.capitalize():\N{smiling face with sunglasses}^15}')
İmage
Gribouillis and scidam like this post
Quote
#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 ?
Quote
#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()
Quote
#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.
Quote
#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.
Quote
#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.
Quote

Top Page

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

Forum Jump:


Users browsing this thread: 1 Guest(s)