Python random non alphabetical character

292 views Asked by At

I am new to python so i am not sure how this would work out. Anyhow, I would like to know how to pick a random non alphabetical character/symbol and simply print it. Just as an example it will randomly pick "æ"

Thanks!

2

There are 2 answers

3
sweber On BEST ANSWER

If you want to print characters from a given sequence, you can use this:

# -*- coding: utf-8 -*-

import random

seq=['☺', '☹', '♡', '♥', '❀', '❃']

for i in range(20):
    print random.choice(seq),

The output is for example:

☹ ♥ ❀ ❀ ❀ ❃ ☺ ♡ ♡ ❃ ❀ ❀ ☺ ☺ ♡ ❃ ♡ ♡ ❃ ❀

Note the -*- coding: utf-8 -*- which tells python that this file is encoded in utf8.

Or this:

for i in range(8592, 8602):
    print unichr(i),

which gives me

← ↑ → ↓ ↔ ↕ ↖ ↗ ↘ ↙

This works quite fine for me.

But note: Your console has to support unicode, and the font has to contain the glyphs for your character. I use Linux, may be, Windows has some problems with unicode?

0
user3802921 On

Okay so print actually hasn't had any problems it was Python all along. I had to update, i had v 3.3