Strings
basic methods
method | outcome |
---|---|
.title() |
changes the string to title case (captialised) |
.upper() |
changes the string to upper case |
.lower() |
changes the string to lower case |
.rstrip() |
strips any whitespaces from the end of a string |
.lstrip() |
strips any whitespaces from the start of a string |
.strip() |
strips any whitespaces from the start and end of a string |
can also give a string of chars to strip, and it will strip those chars from the beginning/end of a string.
concatenation
a = "Hello"
b = "World"
print(a+" "+b)
conversion
answer = 7
print ("The answer is " + str(answer))
The method str()
calls the builtin method __str()__
formatting
Useful reference ... pyformat.info
code | output | notes |
---|---|---|
"%s %s" % ('one', 'two') |
'one two' | old style |
"{} {}".format('one','two') |
'one two' | new style |
"%d %d" % (1, 2) |
1 2 | old style |
"{} {}".format(1, 2) |
1 2 | new style |
"{1} {0}".format('one','two') |
'two one' |