|
5 | 5 | <link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/>
|
6 | 6 | <link href="https://learnbyexample.github.io"/>
|
7 | 7 | <generator uri="https://www.getzola.org/">Zola</generator>
|
8 |
| - <updated>2023-05-16T00:00:00+00:00</updated> |
| 8 | + <updated>2023-05-24T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id>
|
| 10 | + <entry xml:lang="en"> |
| 11 | + <title>Python tip 28: string concatenation and repetition</title> |
| 12 | + <published>2023-05-24T00:00:00+00:00</published> |
| 13 | + <updated>2023-05-24T00:00:00+00:00</updated> |
| 14 | + <link rel="alternate" href="https://learnbyexample.github.io/tips/python-tip-28/" type="text/html"/> |
| 15 | + <id>https://learnbyexample.github.io/tips/python-tip-28/</id> |
| 16 | + <content type="html"><p>Python provides a wide variety of features to work with strings. In this tip, you'll learn about string concatenation and repetition.</p> |
| 17 | +<p>The <code>+</code> operator is one of the ways to concatenate two strings. The operands can be any expression that results in a string value and you can use any of the different ways to specify a string literal. Another option is to use f-strings. Here are some examples:</p> |
| 18 | +<pre data-lang="python" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-python "><code class="language-python" data-lang="python"><span style="color:#72ab00;">&gt;&gt;&gt; </span><span>s1 </span><span style="color:#72ab00;">= </span><span style="color:#d07711;">&#39;hello&#39; |
| 19 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span>s2 </span><span style="color:#72ab00;">= </span><span style="color:#d07711;">&#39;world&#39; |
| 20 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#b39f04;">print</span><span>(s1 </span><span style="color:#72ab00;">+ </span><span style="color:#d07711;">&#39; &#39; </span><span style="color:#72ab00;">+ </span><span>s2) |
| 21 | +</span><span>hello world |
| 22 | +</span><span> |
| 23 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#668f14;">f</span><span style="color:#d07711;">&#39;</span><span>{s1} {s2}</span><span style="color:#d07711;">&#39; |
| 24 | +</span><span style="color:#d07711;">&#39;hello world&#39; |
| 25 | +</span><span> |
| 26 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span>s1 </span><span style="color:#72ab00;">+ </span><span style="color:#668f14;">r</span><span style="color:#d07711;">&#39;</span><span style="color:#aeb52b;">.</span><span style="color:#7c8f4c;"> 1</span><span style="color:#aeb52b;">\n</span><span style="color:#7c8f4c;">2</span><span style="color:#d07711;">&#39; |
| 27 | +</span><span style="color:#d07711;">&#39;hello. 1</span><span style="color:#aeb52b;">\\</span><span style="color:#d07711;">n2&#39; |
| 28 | +</span></code></pre> |
| 29 | +<p>Another way to concatenate is to simply place any kind of string literal next to each other. You can use zero or more whitespaces between the two literals. But you cannot mix an expression and a string literal. If the strings are inside parentheses, you can also use newline characters to separate the literals and optionally use comments.</p> |
| 30 | +<pre data-lang="python" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-python "><code class="language-python" data-lang="python"><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#d07711;">&#39;hello&#39; </span><span style="color:#668f14;">r</span><span style="color:#d07711;">&#39;</span><span style="color:#aeb52b;">.</span><span style="color:#7c8f4c;"> 1</span><span style="color:#aeb52b;">\n</span><span style="color:#7c8f4c;">2</span><span style="color:#d07711;">&#39; |
| 31 | +</span><span style="color:#d07711;">&#39;hello. 1</span><span style="color:#aeb52b;">\\</span><span style="color:#d07711;">n2&#39; |
| 32 | +</span><span> |
| 33 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#b39f04;">print</span><span>(</span><span style="color:#d07711;">&#39;apple&#39; |
| 34 | +</span><span style="color:#b3933a;">... </span><span style="color:#d07711;">&#39;-banana&#39; |
| 35 | +</span><span style="color:#b3933a;">... </span><span style="color:#d07711;">&#39;-cherry&#39;</span><span>) |
| 36 | +</span><span>apple</span><span style="color:#72ab00;">-</span><span>banana</span><span style="color:#72ab00;">-</span><span>cherry |
| 37 | +</span></code></pre> |
| 38 | +<p>You can repeat a string by using the <code>*</code> operator between a string and an integer. You'll get an empty string if the integer value is less than <code>1</code>.</p> |
| 39 | +<pre data-lang="python" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-python "><code class="language-python" data-lang="python"><span style="color:#72ab00;">&gt;&gt;&gt; </span><span>style_char </span><span style="color:#72ab00;">= </span><span style="color:#d07711;">&#39;-&#39; |
| 40 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#b39f04;">print</span><span>(style_char </span><span style="color:#72ab00;">* </span><span style="color:#b3933a;">50</span><span>) |
| 41 | +</span><span style="color:#72ab00;">-------------------------------------------------- |
| 42 | +</span><span> |
| 43 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span>word </span><span style="color:#72ab00;">= </span><span style="color:#d07711;">&#39;buffalo &#39; |
| 44 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#b39f04;">print</span><span>(</span><span style="color:#b3933a;">8 </span><span style="color:#72ab00;">* </span><span>word) |
| 45 | +</span><span>buffalo buffalo buffalo buffalo buffalo buffalo buffalo buffalo |
| 46 | +</span></code></pre> |
| 47 | +<p><strong>Video demo</strong>:</p> |
| 48 | +<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/nuH7C162W5U" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p> |
| 49 | +<br> |
| 50 | +<p><img src="/images/info.svg" alt="info" /> See also my <a href="https://github.com/learnbyexample/100_page_python_intro">100 Page Python Intro</a> ebook.</p> |
| 51 | +</content> |
| 52 | + </entry> |
10 | 53 | <entry xml:lang="en">
|
11 | 54 | <title>Vim tip 26: executing shell commands</title>
|
12 | 55 | <published>2023-05-16T00:00:00+00:00</published>
|
|
0 commit comments