====== Strings und print ======
===== UTF-8 :) =====
# -*- coding: utf-8 -*-
===== Formatter =====
http://learnpythonthehardway.org/book/ex5.html
* ''%d'': signed integer decimal
* ''%s'': string
* ''%r'': raw (for debugging)
# string
my_name = 'Zed A. Shaw'
# integer
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
# string
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
# nicht so interessant
print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
print "He's %d pounds heavy." % my_weight
print "Actually that's not too heavy."
# mehrere Ersetzungen in einem print()
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth
# this line is tricky, try to get it exactly right
# Operationen in der Ersetzung möglich
# Ersetzung ist eine Liste (1, 'Schnack', a, b, a + b)
print "If I add %d, %d, and %d I get %d." % (
my_age, my_height, my_weight, my_age + my_height + my_weight)
http://learnpythonthehardway.org/book/ex6.html\\
https://docs.python.org/3/tutorial/inputoutput.html#fancier-output-formatting (Neu!)\\
# string mit Formatierung und Ersetzung wird als string in x gespeichert
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
# wieder
y = "Those who know %s and those who %s." % (binary, do_not)
print x
print y
# %r mach das Ganze in raw
print "I said: %r." % x
print "I also said: '%s'." % y
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
print joke_evaluation % hilarious
w = "This is the left side of..."
e = "a string with a right side."
# Zusammensetzung von strings mit "+"
# print "Schnick" + 3 geht NICHT, weil string und int
print w + e
# 10 Punkte
print "." * 10
# Format als string speichern
formatter = "%r %r %r %r"
# Format mit verschiedenem Inhalt mischen
# Achtung: Geht nur mit %r, weil string, int (und float)
print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)