{"id":17,"date":"2024-01-24T12:46:09","date_gmt":"2024-01-24T12:46:09","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=17"},"modified":"2024-01-24T12:46:10","modified_gmt":"2024-01-24T12:46:10","slug":"python-string","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-string\/","title":{"rendered":"Python String"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python string and its basic operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python string<\/h2>\n\n\n\n<p>A string is a series of characters. In Python, anything inside quotes is a string. And you can use either single or double quotes. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>message = 'This is a string in Python' message = \"This is also a string\"<\/code><\/pre>\n\n\n\n<p>If a string contains a single quote, you should place it in double-quotes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>message = \"It's a string\"<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>And when a string contains double quotes, you can use the single quotes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>message = '\"Beautiful is better than ugly.\". Said Tim Peters'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To escape the quotes, you use the backslash (<code>\\<\/code>). For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>message = 'It\\'s also a valid string'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The Python interpreter will treat the backslash character (\\) special. If you don\u2019t want it to do so, you can use raw strings by adding the letter\u00a0<code>r<\/code>\u00a0before the first quote. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>message = r'C:\\python\\bin'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Creating multiline strings<\/h3>\n\n\n\n<p>To span a string multiple lines, you use triple-quotes \u201c\u201d\u201d\u2026\u201d\u201d\u201d or \u201d\u2019\u2026\u201d\u2019. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>help_message = ''' Usage: mysql command -h hostname -d database name -u username -p password ''' print(help_message)<\/code><\/code><\/pre>\n\n\n\n<p>It\u2019ll output the following if you execute the program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Usage: mysql command -h hostname -d database name -u username -p password<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using variables in Python strings with the f-strings<\/h2>\n\n\n\n<p>Sometimes, you want to use the values of\u00a0variables\u00a0in a string.<\/p>\n\n\n\n<p>For example, you may want to use the value of the\u00a0<code>name<\/code>\u00a0variable inside the\u00a0<code>message<\/code>\u00a0string variable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>name = 'John' message = 'Hi'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To do it, you place the letter\u00a0<code>f<\/code>\u00a0before the opening quotation mark and put the brace around the variable name:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>name = 'John' message = f'Hi {name}' print(message)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Python will replace the\u00a0<code>{name}<\/code>\u00a0by the value of the\u00a0<code>name<\/code>\u00a0variable. The code will show the following on the screen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Hi John<\/code><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>message<\/code>&nbsp;is a format string, or f-string in short. Python introduced the f-string in version 3.6.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Concatenating Python strings<\/h2>\n\n\n\n<p>When you place the string literals next to each other, Python automatically\u00a0concatenates\u00a0them into one string. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>greeting = 'Good ' 'Morning!' print(greeting)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Good Morning!<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To\u00a0concatenate two string variables, you use the operator\u00a0<code>+<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>greeting = 'Good ' time = 'Afternoon' greeting = greeting + time + '!' print(greeting)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Good Afternoon!<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing string elements<\/h2>\n\n\n\n<p>Since a string is a\u00a0sequence\u00a0of characters, you can access its elements using an index. The first character in the string has an index of zero.<\/p>\n\n\n\n<p>The following example shows how to access elements using an index:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>str = \"Python String\" print(str&#91;0]) <em># P<\/em> print(str&#91;1]) <em># y<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>How it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, create a variable that holds a string&nbsp;<code>\"Python String\"<\/code>.<\/li>\n\n\n\n<li>Then, access the first and second characters of the string by using the square brackets&nbsp;<code>[]<\/code>&nbsp;and indexes.<\/li>\n<\/ul>\n\n\n\n<p>If you use a negative index, Python returns the character starting from the end of the string. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>str = \"Python String\" print(str&#91;-1]) <em># g<\/em> print(str&#91;-2]) <em># n<\/em><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The following illustrates the indexes of the string\u00a0<code>\"Python String\"<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>+---+---+---+---+---+---+---+---+---+---+---+---+---+ | P | y | t | h | o | n | | S | t | r | i | n | g | +---+---+---+---+---+---+---+---+---+---+---+---+---+ 0 1 2 3 4 5 6 7 8 9 10 11 12 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Getting the length of a string<\/h2>\n\n\n\n<p>To get the length of a string, you use the\u00a0<code>len()<\/code>\u00a0function. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>str = \"Python String\" str_len = len(str) print(str_len)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>13<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Slicing strings<\/h2>\n\n\n\n<p>Slicing\u00a0allows you to get a substring from a string. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>str = \"Python String\" print(str&#91;0:2])<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Py<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>str[0:2]<\/code>&nbsp;returns a substring that includes the character from the index 0 (included) to 2 (excluded).<\/p>\n\n\n\n<p>The syntax for slicing is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>string&#91;start:end]<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The substring always includes the character at the&nbsp;<code>start<\/code>&nbsp;and excludes the string at the&nbsp;<code>end<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>start<\/code>&nbsp;and&nbsp;<code>end<\/code>&nbsp;are optional. If you omit the&nbsp;<code>start<\/code>, it defaults to zero. If you omit the&nbsp;<code>end<\/code>, it defaults to the string\u2019s length.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python strings are immutable<\/h2>\n\n\n\n<p>Python strings are\u00a0immutable. It means that you cannot change the string. For example, you\u2019ll get an error if you update one or more characters in a string:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>str = \"Python String\" str&#91;0] = 'J'<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Traceback (most recent call last): File \"app.py\", line 2, in &lt;module> str&#91;0] = 'J' TypeError: 'str' object does not support item assignment&lt;\/module><\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When want to modify a string, you need to create a new one from the existing string. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>str = \"Python String\" new_str = 'J' + str&#91;1:] print(new_str)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Jython String<\/code><\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python string and its basic operations. Introduction to Python string A string is a series of characters. In Python, anything inside quotes is a string. And you can use either single or double quotes. For example: If a string contains a single quote, you should place it in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-1-fundamentals"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":18,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/17\/revisions\/18"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}