1

I thought it would be fun to code all the different ways to show Hello World on the display.

Let's start simple ...
print("Hello World")

Can anybody print out "Hello World" vertically?

Edited by vegaseat

8
Contributors
48
Replies
300
Views
2 Years
Discussion Span
Last Post by ZZucker
Featured Replies
  • 1

    Who ever heard of Caesar Cipher ... encrypted = "Ifmmp!Xpsme" print("".join(chr(ord(x)-1) for x in encrypted)) Anybody ready to use a GUI with a fancy font? Read More

  • 1

    > print("Hello World") > > Can anybody print out "Hello World" vertically? print('\n'.join("Hello World")) Read More

  • 1

    Hi, double newlines seemed to work in Windows, but rotating the text turned out to be little more involved than I thought. I even had to revise my [earlier rotation code](https://www.daniweb.com/software-development/python/code/371536/rotate-sequences-left-or-right) to use izip_longest to deal with not square rotation of the character matrix. try: from itertools import izip_longest except … Read More

  • 3

    Vegaseat: Bit more interesting content decorator: def print_it(func): def inner(*args): print(' '.join(func())) return func return inner @print_it def my_func(): return "Hello", "World" my_func() Read More

  • 2

    You can of course use a generator expression to create a generator ... gen = (c for c in "Hello World") while True: try: print(next(gen), end='') except StopIteration: print('') break Read More

0

Now for something completely different, slicing ...
print("dlroW olleH"[::-1])

Edited by vegaseat

0

Mildly more complex using the ASCII values ...

hello_list = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
print("".join(chr(x) for x in hello_list))

Edited by vegaseat

1

Who ever heard of Caesar Cipher ...

encrypted = "Ifmmp!Xpsme"
print("".join(chr(ord(x)-1) for x in encrypted))

Anybody ready to use a GUI with a fancy font?

Edited by vegaseat

0

Oh Gee, the response is overwhelming, here is one GUI attempt ...

# for Python2 change tkinter to Tkinter
import tkinter as tk

root = tk.Tk()
mytext = " Hello World "
myfont = ('times', 48, 'bold')
label = tk.Label(root, text=mytext, font=myfont, fg='red', bg='yellow')
label.pack()
root.mainloop()

I don't think Tkinter looks homely all the time.
IMHO, Tkinter looks best in OSX.

Edited by vegaseat

1

print("Hello World")

Can anybody print out "Hello World" vertically?

print('\n'.join("Hello World"))
0

Vertical slanted left to right ...

print('\n'.join(" "*ix + c for ix, c in enumerate("Hello World")))

Now if I only could slant it right to left.

Edited by vegaseat

0

Pyside/PyQT allows you to use HTML code ...

from PySide.QtGui import *

app = QApplication([])

s_html = '''\
<font face="Courier" color=red size=7><p><i> {} </i></p></font>
'''.format("Hello World")
label = QLabel(s_html)
label.show()

app.exec_()
0

left to right but it's increasing upwards :P

print '\n'.join(" "*ix + c for ix, c in reversed(list(enumerate("Hello World"))))
pyTony's version backwards vertically

print('\n'.join("Hello World"))[::-1]

Edited by Slavi

0

Some less common string methods anyone?

s = "hello world"
print(" ".join(word.capitalize() for word in s.split()))
# simpler ...
print(s.title())
# more ...
print(s.title().center(20))
print(s.title().swapcase())

Edited by vegaseat

0

Here little bigger version

hello = dict(zip('helo wrd'.upper(),'''
 *  *
 *  *
 ****
 *  *
 *  *

 ****
 *
 ****
 *
 ****

 *
 *
 *
 *
 ****

  ***
 *   *
 *   *
 *   *
  ***






*  *  *
*  *  *
*  *  *
 *  *

 ****
 *   *
 *  *
 ***
 *  *
 *   *

 ***
 *  *
 *  *
 *  *
 ***
'''.split('\n\n')))


print('\n\n'.join(hello[c] for c in 'Hello world'.upper()))
0

This one looks better with python 3

import cgitb, os, sys, tempfile, webbrowser

dst = os.path.join(tempfile.gettempdir(), 'helloworld.html')
try:
    raise type('Hello World', (Exception,),{})
except:
    with open(dst, 'w') as ofh:
        ofh.write(cgitb.html(sys.exc_info()))

webbrowser.open(dst) 
0

IDLE does not like lines only space characters, so here safer version that works.

hello = dict(zip('helowrd'.upper(),'''
 *  *
 *  *
 ****
 *  *
 *  *

 ****
 *
 ****
 *
 ****

 *
 *
 *
 *
 ****

  ***
 *   *
 *   *
 *   *
  ***

*  *  *
*  *  *
*  *  *
 *  *

 ****
 *   *
 *  *
 ***
 *  *
 *   *

 ***
 *  *
 *  *
 *  *
 ***
'''.split('\n\n')))

hello[' '] = 5 * '\n'
print('\n\n'.join(hello[c] for c in 'Hello world'.upper()))

Edited by pyTony

0

@pyTony, for some odd reason the '\n' only line does not copy and paste well on my Linux machine using IDLE, so I modified your clever code like this ...

''' hello_world_pytony1.py
use "\n~" as delimiter
'''

hello = dict(zip('helowrd'.upper(),'''
 *  *
 *  *
 ****
 *  *
 *  *
~
 ****
 *
 ****
 *
 ****
~
 *
 *
 *
 *
 ****
~
  ***
 *   *
 *   *
 *   *
  ***
~
*  *  *
*  *  *
*  *  *
 *  *
~
 ****
 *   *
 *  *
 ***
 *  *
 *   *
~
 ***
 *  *
 *  *
 *  *
 ***
'''.split('\n~')))

hello[' '] = 5 * '\n'
#print(hello)  # test
print('\n\n'.join(hello[c] for c in 'Hello world'.upper()))

Now it copies and pastes well.

Edited by vegaseat

1

Hi, double newlines seemed to work in Windows, but rotating the text turned out to be little more involved than I thought. I even had to revise my earlier rotation code to use izip_longest to deal with not square rotation of the character matrix.

try:
    from itertools import izip_longest
except ImportError:
    # Python 3
    from itertools import zip_longest as izip_longest

def rot_right(a, fillvalue=None):
    return list(izip_longest(*a[::-1], fillvalue=fillvalue))


hello = dict(zip('helowrd'.upper(),'''
 *  *
 *  *
 ****
 *  *
 *  *
§
 ****
 *
 ****
 *
 ****
§
 *
 *
 *
 *
 ****
§
  ***
 *   *
 *   *
 *   *
  ***
§
*  *  *
*  *  *
*  *  *
 *  *
§
 ****
 *   *
 *  *
 ***
 *  *
 *   *
§
 ***
 *  *
 *  *
 *  *
 ***
'''.split('\n§')))

hello[' '] = 5 * '     \n'
print('\n\n'.join(hello[c] for c in 'Hello world'.upper()))

print('Rotated:')
for c in 'Hello world'.upper():
    print('\n'.join(''.join(data)
                    for data in rot_right(hello[c].split('\n'),
                                          fillvalue=' ')
                    ))

Edited by pyTony: simplified

0

@pyTony hi , I ran your code in python3 , it does not like izip_longest but it was fine with python2 . what should we use instead ?

1

I think in Python3 it is called zip_longest. I updated my code in original post.

Edited by pyTony

0

@pyTony , hi , thank you for your codes . now I think it would be a great idea for this project to move 'hello world ' across the screen like an animated banner without using graphic packages
I have no idea how to do it so your help (anybody's help ) is appreciated .

0

Here is my code changed to produce text mode banner effect by os independent way of printing empty lines for clearing the screen. This method suprizingly seems to work better for IDLE window than terminal (console) screen/window.

# -*- coding: cp1252 -*-
from __future__ import print_function
import time
try:
    from itertools import izip_longest
except ImportError:
    # Python 3
    from itertools import zip_longest as izip_longest

def rot_right(a, fillvalue=None):
    return list(izip_longest(*a[::-1], fillvalue=fillvalue))


hello = dict(zip('helowrd'.upper(),'''

 *  *
 *  *
 ****
 *  *
 *  *
§

 ****
 *
 ****
 *
 ****
§

 *
 *
 *
 *
 ****
§

  ***
 *   *
 *   *
 *   *
  ***
§

*  *  *
*  *  *
*  *  *
*  *  *
 *  *
§

 * *  
 *  *
 ***
 *  *
 *   *
§

 ***
 *  *
 *  *
 *  *
 ***
'''.split('\n§')))

hello[' '] = 5 * '     \n'
spacing = 10
# at least lines per terminal minus banner hight
# or less for multibanner effect
much = 25 - 6
banner_length = 80
banner = ['' for line in range(8)]
for c in 'Hello world'.upper():
    print(c)
    for n, line in enumerate(hello[c].split('\n')):
        print(n,line)
        banner[n] += line.ljust(spacing)
    n += 1
    while n < len(banner):
        banner[n] += ' '.ljust(spacing)
        n += 1


for rot in range(480):
    rot = rot % banner_length
    time.sleep(0.1)
    print('\n' * much) # system independant emptying of screen
    print('\n'.join((b[rot:] + spacing*' ' + b[:rot])[:banner_length] for b in banner))
0

@pyTony hi , again , thanks for the code . when I run it on idle I get two lines moving nicely and two other lines moving very very fast but these two fast lines do not show up when run in the terminal , I am guessing idle does not clear the screen fast enough

0

Using Java libraries with Jython ...

from javax.swing import JFrame

f = JFrame('Hello, World!')
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
f.setSize(300, 300)
f.setLocationRelativeTo(None)
f.setVisible(True)
1

Using Windows .NET or Linux Mono with IronPython ...

import clr

clr.AddReference("System.Windows.Forms")
clr.AddReference('System.Drawing')

from System.Windows.Forms import Application, Form
from System.Drawing import Color

class IForm(Form):

    def __init__(self):
        # set form title, bg_color, size, position
        self.Text = 'Hello World'
        self.BackColor = Color.Green
        self.Width = 250
        self.Height = 200
        self.CenterToScreen()

Application.Run(IForm())
0
# this will move 'hello world ' across the window horizontally
import time
import sys
if sys.version[0]=='3':
    from tkinter import *
else:
    from Tkinter import *

root = Tk()

canvas = Canvas(root, width=500, height=400)
canvas.pack()
txt=canvas.create_text(110,200,text='hello world', fill='Green',font=('Helvetica',30,'bold'))
y = x = 5

for k in range(50):
    time.sleep(0.15)
    canvas.move(txt, x,0)
    canvas.update()

root.mainloop()
0

well , I am pretty sure people in Germany do not use "hello world" so here is my code translated to German (hallo welt)
and since I am using random , you will get different results

import time
import random
import sys
if sys.version[0]=='3':
    from tkinter import *
else:
    from Tkinter import *

root=Tk()
root.title('hello world in German')
canvas = Canvas(root,width=400, height=400, relief = RAISED,bg = 'blue')
canvas.pack()


txt2=canvas.create_text(100,300,text='hallo welt',fill='Yellow',font=('Helvetica',30,'bold'))

x1=random.randint(0,5)
y1=random.randint(0,5)
for x in range(50):
    canvas.move(txt2, x1, -y1)
    canvas.after(30)
    canvas.update()


root.mainloop()
0

A small improvement !

import time
import random
import sys
if sys.version[0]=='3':
    from tkinter import *
else:
    from Tkinter import *

root=Tk()
root.title('hello world in German')
canvas = Canvas(root,width=400, height=400, relief = RAISED,bg = 'blue')
canvas.pack()


txt2=canvas.create_text(100,300,text='hallo welt',fill='Yellow',font=('Helvetica',30,'bold'))


x1, y1 = (random.randint(1,6) for i in range(2))

while True:
    canvas.move(txt2, x1, y1)
    canvas.after(30)
    canvas.update()
    bl, bt, br, bb = canvas.bbox(txt2)
    if bl < 0 or br > 400 or bt < 0 or bb > 400:
        x1, y1 = (random.randint(1,6) for i in range(2))
    if bl < 0:
        x1 = abs(x1)
    elif br > 400:
        x1 = -abs(x1)
    if bt < 0:
        y1 = abs(y1)
    elif bb > 400:
        y1 = -abs(y1)

root.mainloop()
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.