Some useful special characters:
new_line = "\n" #
horizontal_tab = "\t" # can modify its width
rep = "\\" # backslash
one_quotion = "\'" # '
double_quotion = "\"" # "
############################# these come from the typewriter era
bell = "\a" # alert, this actually beeps on some computers (hardly supported now)
carriage_return = "\r" # jumps to the beginning of the same row
vertical_tab = "\v" #
print "Tabular:\n\nFrom\there\n-------------------------\nTo \tthere"
Substring:
s = "Firstsecondthird"
print s[:5]
print s[5:11]
print s[11:]
Concatenation (string to string):
s = "puppy"
print "5 " + s
print 5 + s
s = "puppy"
quantity = 5
print str(quantity) + " " + s
s = "some LOWER-, some UPPERcase letters"
The following method converts every character lowercase, except the first, that becomes capital:
s.capitalize()
All capitals:
s.upper()
All lowercase:
s.lower()
Checks whether the string contains only alphabetic characters (comma and whitespace is not):
s.isalpha()
You can check whether one string contains an other and also its actual position (index):
'case' in s
s.find('case')
You can split a string into a list of strings with the split
method.
The string is split by the whitespaces by default (tab, space, newline ...).
s.split() # split by whitespaces
s = "first second\tthird\nfourth"
s.split()
s = "first, second, third, fourth"
s.split(", ") # split by a given separator (a comma and a space)
strip
¶The following method erases the whitespace characters from the beginning and the end:
s = ' \t white spaces \t \n\n '
s.strip()
Strips given characters:
s = "...once uppon a time,"
print s.strip(".,") # strips both sides
print s.rstrip(".,") # strips from the end
print s.lstrip(".,") # strips from the beginning
s = "where"
print '0123456789'*3
print s.center(30)
print s.rjust(30)
print s.ljust(30)
The parameter of the method tells the final width.
You can print a table nicely:
tabular = [["First row", -2, -310], ["Second row", 3, 1], ["Third row",-321, 11]]
tabular_string = ""
for row in tabular:
tabular_string += row[0].ljust(13)
for i in range(1, len(row)):
tabular_string += str(row[i]).rjust(7)
tabular_string += "\n"
print tabular_string
format
method¶The object is the formatting string and the parameters are the things to subtitute.
The numbers in the brackets mark the parameters.
'{0}-{1}-{2} {0}, {1}, {2}, {0}{0}{0}'.format('X', 'Y', 'Z')
The format marker "{ }"
can have optional formatting instructions: {number:optional}
optional | Meaning |
---|---|
d | decimal |
b | binary |
o | octal |
x, X | hex, capital HEX |
f, F | float |
e, E | exponential form: something times 10 to some power |
< | left justified |
> | right justified |
^ | centered |
c^ | centered but with a character 'c' as padding |
print "01234 01234 01234 0123456789"
print '{0:5} {1:5d} {2:>5} {3:*^10}'.format('0123', 1234, '|', 'center')
"int {0:d}, hex {0:x} {0:X}, oct {0:o}, bin {0:b}".format(42)
"{0}, {0:e}, {0:f}, {0:8.4f}, {0:15.1f}".format(-12.345)
You can also name the parameters, it is more convinient then indices.
'The center is: ({x}, {y})'.format(x=3, y=5)
x1 = 3; y1 = 4
print 'The center is: ({x}, {y})'.format(x=x1, y=y1)
table_string = ""
for row in tabular:
table_string += "{0:_<13}".format(row[0])
for i in range(1, len(row)):
table_string += "{0:>7d}".format(row[i])
table_string += "\n"
print table_string
You have to import these function, because they are not default. Put this in the beginning of your code.
import re
The findall
function finds all matching substrings in a string:
pattern = r'[0-9]+'
string = "Once uppon a time there was 1 little puppy and 7 dwarfs"
print re.findall(pattern, string)
Since backslash and other special characters can be used in a RegEx pattern, you have to be careful with them.
The best if you use a so called raw string as pattern. In this format one backslash means actually one backspash. You don't have to escape the backslash.
If you put an r
in front of the string, then it is in a raw format.
raw_string = r"aa\txx\s"
not_raw = "aa\txx\\"
print "Raw: " + raw_string
print "Not raw: " + not_raw
You can even substitute patterns.
pattern = r'[0-9]+'
substitute = r'12'
string = "Once uppon a time there was 1 little puppy and 7 dwarfs"
print re.sub(pattern, substitute, string)
You can group the patterns and refer to them as numbers in the substitution: \1
pattern = r'[0-9]+\s([a-z]+)'
substitute = r'12 \1'
string = "Once uppon a time there was 1 little puppy and 7 dwarfs"
print re.sub(pattern, substitute, string)