Strings and print#
Strings#
Python allows both single- and double-quotes to be used with strings. It does not distinguish between a string and a character (like C++ does). For example:
s1 = "this is a string"
s2 = 'this is also a string'
If we look at the type, type(s1), we would see that it is a str object.
Just like C++ has a lot of functions that work on strings, python has a large library for strings. We can do:
help(str)
to see these functions.
Format strings#
C++’s std::format was inspired by python and uses the same formatting. In python, we invoke
the format() function on a string (it is a member function of the string class), so we can do:
a = 1
b = 2.5
c = "string"
print("a = {:2}, b = {:.3f}, c = {:10}".format(a, b, c))
but python also has f-strings
which allow us to put the values directly into the { } in the format string.
print(f"a = {a:2}, b = {b:.3f}, c = {c:10}")
Important
An f-string has the form: f" ... " —with a leading f.
print#
python doesn’t have a stream operator like C++’s <<. Instead we use print. By
default print adds a newline, but we can suppress this by passing the end="" argument
to print, e.g.:
print("this is print without a newline", end="")
see help(print) for more information.