9 Replies - 3206 Views - Last Post: 27 December 2013 - 08:24 AM Rate Topic: -----

#1 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

[Help] How do you write superscript?

Posted 23 October 2012 - 06:56 AM

Well, I am trying to make a user friendly program with python and I am wondering how do you write superscript numbers or letters in python. In addition is it possible to write subscript also? In other words how can I output my program to print x2?
Is This A Good Question/Topic? 0
  • +

Replies To: [Help] How do you write superscript?

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: [Help] How do you write superscript?

Posted 23 October 2012 - 07:02 AM

You mean output to a console output? Have you tried to google it?
Was This Post Helpful? -2
  • +
  • -

#3 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 63
  • Joined: 07-October 12

Re: [Help] How do you write superscript?

Posted 23 October 2012 - 07:09 AM

Yes, I have tried finding on Google and seems most of them are outdated from range 2008-2010. I need the newest version Python, to output on the console. My Python is 3.3.0
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: [Help] How do you write superscript?

Posted 23 October 2012 - 07:19 AM

It depends where you are outputting the data and what output formats can be accepted.

Try printing the character unicode (0x00B2) which is the superscripted 2, to the console and see if it shows up:

>>> print(chr(0x00B2))
'²'


This post has been edited by Simown: 23 October 2012 - 07:23 AM

Was This Post Helpful? 3
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: [Help] How do you write superscript?

Posted 23 October 2012 - 07:50 AM

View Postdarek9576, on 23 October 2012 - 10:02 AM, said:

You mean output to a console output? Have you tried to google it?

Did you try googling it before you advised him to? I just googled write subscript characters in python (which is a perfectly reasonable thing for someone in his position to try) and my results weren't very helpful.


Wikipedia has a great page on unicode subscripts and superscripts that should also prove very helpful.

This post has been edited by atraub: 23 October 2012 - 07:50 AM

Was This Post Helpful? 2
  • +
  • -

#6 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: [Help] How do you write superscript?

Posted 23 October 2012 - 08:15 AM

Why would i? It ain't my problem is it?
Was This Post Helpful? -5
  • +
  • -

#7 SegFaulty  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 35
  • Joined: 11-October 10

Re: [Help] How do you write superscript?

Posted 23 October 2012 - 11:35 AM

Are you needing to print superscript for math? Because if you are having issues using the unicode you could also print out x^2 as well.
Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: [Help] How do you write superscript?

Posted 23 October 2012 - 11:38 AM

View Postdarek9576, on 23 October 2012 - 11:15 AM, said:

Why would i? It ain't my problem is it?
If it's not your problem whether or not someone actually finds solutions, perhaps you're on the wrong website.

EDIT:ahhh crap, accidentally up-rep'd his first one, there really needs to be a way to undo reps... crappy touchpad.

This post has been edited by atraub: 23 October 2012 - 11:45 AM

Was This Post Helpful? 2
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7283
  • View blog
  • Posts: 12,078
  • Joined: 19-March 11

Re: [Help] How do you write superscript?

Posted 23 October 2012 - 11:41 AM

View Postdarek9576, on 23 October 2012 - 10:15 AM, said:

Why would i? It ain't my problem is it?



So why did you answer?

@OP: As a rule, the safest course is to limit yourself to straight text when writing to the console, since even if your local console can display formatted text, using that capacity limits your program to running on those consoles.

To write to a document in a format that allows sub/superscript, you'd need to research the format and either generate the markup (ie, a [sup] tag here at DIC, or $a_x b^y$ in latex) or using a tool (ie, use Microsoft's COM to spit docx)

This post has been edited by jon.kiparsky: 23 October 2012 - 11:49 AM

Was This Post Helpful? 2
  • +
  • -

#10 DK3250  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 40
  • Joined: 27-December 13

Re: [Help] How do you write superscript?

Posted 27 December 2013 - 08:24 AM

Allow me to take up this rather old thread.
As a Python beginner I encountered the problem of subscript / superscript in writing chemical formulas.
I made this small program that you can easily modify to your needs.
The program should be implemented as a function in your code.
I'm sure many of you more experienced people can improve - but at least this works.



import pygame
pygame.init()

# any text following '<' will print as subscript
# any text following '>' will print as superscript
# any text following '%' will print as normal

txt = "SO<4>--%  Normal<Subscript>Superscript"

BLUE = (0, 0, 255)
WHITE =(255, 255, 255)
font_1 = pygame.font.SysFont("comicsansms",  26)
font_2 = pygame.font.SysFont("comicsansms",  16)

screen = pygame.display.set_mode((630, 420))
screen.fill(WHITE)

x_pos = 100
y_pos = 100
sub = 0
font = font_1

for i in range(len(txt)):
    letter = txt[0]
    
    if letter in ['<', '>', '%']:
        if letter == '<':
            sub = 10
            font = font_2
        elif letter == '>':
            sub = -10
            font = font_2
        else:
            sub = 0
            font = font_1
        txt = txt[1:]
        continue

    textSurf = font.render(letter, 1, BLUE)
    textRect = textSurf.get_rect()
    textRect.midleft = [x_pos, y_pos+sub]
    screen.blit(textSurf,textRect)    
    x_pos = x_pos + textRect.width
    txt = txt[1:]

pygame.display.flip()




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1