Doing Repeated Concatenation With the Star Operator
00:00 When applied to strings, the star operator is also known as the repeated concatenation operator, and technically, that character is called an asterisk, but technically asterisk comes from the Greek for small star.
00:12 So because “star” is a bit easier to say, that’s what I’ll go with. The repeated concatenation operator is used to repeat a string a given number of times.
00:21
It takes two arguments, a string and an integer. The integer represents the number of times to repeat the string, and just like the regular concatenation operator, it can also be applied in its augmented form by adding an equal sign, *=
.
00:36
Now for some examples. To the REPL we go. Let’s start with basic usage of the operator. Create a variable called repeated
and in it store a string repeated three times:
00:49
repeated = "Hey, listen "
with a space at the end, * 3
. Now examine repeated
. It’s a new string which contains the contents of the original string, repeated three times.
01:03
To see the augmented version of the operator in action, write the following: repeated *= 5
.
01:10
Now look at repeated
. We have ‘Hey! Listen’ repeated a total of 15 times because the first repeated string has itself been repeated five more times.
01:21 Okay, you’re thinking, great. What’s a practical use for this? Well, for example, this could be part of a terminal utility for viewing or previewing database tables, CSV files, something like that.
01:33
So start by defining some data and call it data
.
01:38
data
is a list of lists, and each list contains three strings. The first list would be a header and it contains the strings “Name”, “Age”, and “Hometown”.
01:49 The next list would be the first row of data containing the strings, “Alice”, “25”, and “New York”. And then one more row of data, the strings, “Charlie”, “35”, and “Chicago”.
02:03
Now write the function display_table()
: def display_table(data):
with a parameter called data
. Next, we define a max_len
variable.
02:14
max_len = [max(len(header)
for header in data[0])]
. This calls the max()
function, and into it passes a generator expression that yields the length of each element in the first row of data.
02:30
As a result, max_len
is now an integer representing the length of the longest string in the header row.
02:38
The next line is where the star operator is useful: sep = "-" * max_len
. This multiplies a dash by the length of the longest element in the header.
02:50
And now for displaying data, you enter a for
loop and you’ll also get to practice using the .join()
method. for row in data:
02:59
print("|".join(header.
ljust(max_lens) for header in row))
. Let me explain this line. You’re using a generator expression to iterate over the row.
03:13
For each string value in the row, you justify the string to the left using the .ljust()
method, which also pads the string if it is shorter than max_len
.
03:22
Then you join the elements into one complete string separated by pipe characters and print it. The next line is a conditional: if row
== data[0]:
.
03:34
This checks if the row is the first row, that is the header row. If so, the next line of code runs: print("|"
.join(sep for _ in row))
.
03:47
This generator expression repeats the sep
variable we defined above once for every element in the row. Because those elements go unused, we actually follow the convention of using an underscore as the variable name.
03:59
The repeated sep
variables are then joined together, separated by pipe characters into one whole string and then printed. And that’s the display_table()
function.
04:10
Let’s see it in action. Call display_table
and pass it the data
variable.
04:20 Hey, pretty nice output. This prints four lines. The first line is the header row justified and separated by pipes: “Name”, “Age”, “Hometown”. The next line is three series of dashes facilitated by the repeated concatenation operator separated by pipes.
04:37 And the last two lines represent rows of data: “Alice”, “25”, “New York”, “Charlie”, “35”, “Chicago”.
04:46
You can see now that by combining techniques like the star operator and the join()
method, you’re able to produce some really nice looking text.
04:53 But in the next lesson, you’ll learn a magic trick: how to combine strings without using operators at all. See you there.
Become a Member to join the conversation.