What is the print(' a') function in python

Asked

Viewed 659 times

0

I was told that the function print('\a') of this code produces a beep, but at the time when I run into pycharm there is no beep.

I would like to know what this function is for. And if you could send me a kind of list of print functions, example print('\n').

#!/usr/bin/python

from datetime import datetime, timedelta

from sys import stdout

from time import sleep

print ('Cronometro regressivo | programador => mmxm')

segundos = int(input('Digite a quantidade de segundos: '))

tempo = timedelta(seconds=segundos)

print ('\n')

while (str(tempo) != '0:00:00'):

    stdout.write("\r%s"%tempo)

    stdout.flush()

    tempo = tempo - timedelta(seconds=1)

    sleep(1)

stdout.write("\r0:00:00")

stdout.flush()

print ('\a')
  • Tried to run the code directly on the terminal?

  • I put this function on the terminal as you suggested, but there occurred the beep, only a blue dot appeared on the screen.

2 answers

3

This probably only works on some terminals because the developers keep it for some interest or compatibility and of course this can only run in terminal, if you try to run elsewhere, like pycharm, it will at most print on the output this: <0x07>

In terminal emulators it usually doesn’t work, but I believe xterm function, depending on the distro, I cannot say for all, as I said, it depends on the developers to maintain compatibility with this or not.

The name of this character is BEL (ASCII Bell), his code in ASCII is 7, then probably its Windows equivalent would be the ^G (the ^ is the escape), but I believe this or depends on the service Beep or the motherboard, I imagine this came at a time when soundcards were not common in computers, yes there were times like this.

Note that on x86 Pcs this depended on the 8254 programmable interval timer chip and probably later came to work based on sound cards, which may be as a service for this, so I read on Windows x64 systems this was removed (in Win10 I can’t say, but it was probably also removed, comment if you find any information about this).

To conclude, the \a will depend on many situations to be able to work and if you want to use this maybe it is better to rethink on a simpler and more current solution, if you want in Python there is this lib: https://github.com/kindlychung/pybeep, install using Pip:

pip install pybeep

1

Use:

print("\a") # Python 3

or

print "\a"  # Python 2

or Bash Shell:

echo $'\a'

Obs: on some terminals the campaign may be disabled.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.