{"id":74652,"date":"2019-12-31T11:47:23","date_gmt":"2019-12-31T06:17:23","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=74652"},"modified":"2024-08-22T18:02:37","modified_gmt":"2024-08-22T12:32:37","slug":"python-strings","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-strings\/","title":{"rendered":"Python Strings &#8211; Get ready to work with sequence of characters"},"content":{"rendered":"<p>In this article, we will delve into Python strings, which are sequences of characters similar to character arrays in C++. We&#8217;ll cover the fundamentals of declaring strings, indexing and slicing them to access and manipulate specific parts, and formatting them for various uses. Additionally, we&#8217;ll explore how to delete strings and make use of Python&#8217;s rich set of built-in methods and functions that make string manipulation straightforward and powerful. Whether you&#8217;re new to programming or experienced, this guide will help you master working with strings in Python.<\/p>\n<h3>What are Strings in Python?<\/h3>\n<p>Python does not have arrays like C++ or Java. It has <strong>lists<\/strong> and <strong>strings<\/strong>.<\/p>\n<p>Lists are <strong>collections<\/strong> of <strong>values<\/strong> and are <strong>mutable<\/strong>. Strings are <strong>sequences<\/strong> of characters and are immutable.<\/p>\n<p>Let\u2019s start with an example.<\/p>\n<p>\u2018Drag me to hell\u2019. This is a <strong>string<\/strong>, we surround it in single quotes here.<\/p>\n<h3>Declaring Python String<\/h3>\n<h4>1. String literals in Python<\/h4>\n<p>String literals are surrounded by <strong>single quotes<\/strong> or <strong>double-quotes<\/strong>.<\/p>\n<p>\u2018Drag me to hell\u2019<\/p>\n<p>\u201cDrag me to hell\u201d<\/p>\n<p>You can also surround them with <strong>triple quotes<\/strong> <strong>(groups of 3 single quotes or double quotes)<\/strong>.<\/p>\n<p>&#8220;&#8221;&#8221;Drag me to hell&#8221;&#8221;&#8221;<br \/>\n&#8221;&#8217;Drag me to hell&#8221;&#8217;<\/p>\n<p>Or using <strong>backslashes<\/strong>.<\/p>\n<p>&gt;&gt; &#8216;Hello\\<br \/>\nHow are you?&#8217;<br \/>\n&#8216;HelloHow are you?&#8217;<\/p>\n<p>You cannot <strong>start<\/strong> a <strong>string<\/strong> with a <strong>single quote<\/strong> and <strong>end<\/strong> it with a <strong>double quote<\/strong> (and vice versa).<\/p>\n<p>But if you surround a string with single quotes and also want to use single quotes as part of the string, you have to escape it with a<strong> backslash (\\)<\/strong>.<\/p>\n<p>\u2018Drag madam\\\u2019s son to hell\u2019<\/p>\n<p>This statement causes a <strong>SyntaxError<\/strong>:<\/p>\n<p>\u2018Drag madam\u2019s son to hell\u2019<\/p>\n<p>You can also do this with double-quotes. If you want to ignore <strong>escape sequences<\/strong>, you can create a <strong>raw string<\/strong> by using an <strong>\u2018r\u2019 or \u2018R\u2019 prefix<\/strong> with the string.<\/p>\n<p>Common escape sequences:<\/p>\n<ul>\n<li>\\\\ Backslash<\/li>\n<li>\\\u2019 Single quotes<\/li>\n<li>\\\u201d Double quotes<\/li>\n<li>\\n Linefeed<\/li>\n<li>\\t Horizontal tab<\/li>\n<\/ul>\n<p>We can assign a string to a <strong>variable<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">name=\u2019Ayushi\u2019<\/pre>\n<p>You can also create a string with the <strong>str() function<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; str(123)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;123&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; str('Hi')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;Hi&#8217;<\/div>\n<h3>Indexing Strings in Python<\/h3>\n<p>We can access a single character in a string through <strong>indexing<\/strong> it. For this, we use <strong>square brackets<\/strong> and an <strong>index<\/strong> or the <strong>position<\/strong> of that character in the string.<\/p>\n<p>Indexing starts at <strong>0<\/strong>, not <strong>1<\/strong>.<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-string-indexing.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75026 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-string-indexing.jpg\" alt=\"python string indexing\" width=\"636\" height=\"268\" \/><\/a><\/p>\n<h4>1. Positive Indexing in Python<\/h4>\n<p>If we perform indexing <strong>left to right <\/strong>in the above python string, we have the indices <strong>0<\/strong> to <strong>19<\/strong>. We have <strong>20 characters<\/strong>. Even the <strong>space<\/strong> and <strong>exclamation mark<\/strong> are <strong>characters<\/strong>.<\/p>\n<p>So to access the letter b, we can type this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; text[7]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\u2018b\u2019<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; text[21]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<b>\u00a0\u00a0<\/b>File &#8220;&lt;pyshell#65&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<b>\u00a0\u00a0\u00a0\u00a0<\/b>text[21]<br \/>\nIndexError: string index out of range<\/div>\n<h4>2. Negative Indexing in Python<\/h4>\n<p>Negative indexing allows you to access characters in a string from right to left. In this system, the index -1 refers to the last character of the string, -2 refers to the second last, and so on. This feature is particularly useful for quickly accessing the end of a string without needing to know its length. For example, in the string &#8220;Python&#8221;, the index -1 would return &#8216;n&#8217; and the index -2 would return &#8216;o&#8217;. Negative indexing provides a convenient way to work with elements at the end of a sequence efficiently.<\/p>\n<p>To get the letter \u2018b\u2019, we will use the -13 index.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt;text[-13]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\u2018b\u2019<\/div>\n<h3>Slicing Strings in Python<\/h3>\n<p>We can get <strong>parts<\/strong> of strings at one time by slicing it with the <strong>slicing operator [:]<\/strong>.<\/p>\n<p>Let us see how to do this.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; text[2:7]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;ushi &#8216;<\/div>\n<p><strong>[2:7]<\/strong> is a <strong>slice<\/strong> that gives us the characters from<strong> index 2<\/strong> to <strong>index 6<\/strong>, <strong>index 7<\/strong> is <strong>not included<\/strong>.<\/p>\n<p>The following are different slices for this string:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; text[:] #complete string\r\n'Ayushi bought a pen!'\r\n&gt;&gt;&gt; text[:7] #beginning to index 6\r\n'Ayushi '\r\n&gt;&gt;&gt; text[3:] #index 3 to end\r\n'shi bought a pen!'\r\n&gt;&gt;&gt; text[-2:-7] #-2 to -7 left to right is nothing\r\n''\r\n&gt;&gt;&gt; text[-7:-2] #index -7 to -1 left to right\r\n' a pe'<\/pre>\n<p>We can also include the <strong>number of steps<\/strong> it should take while traversing.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; text[::2]\r\n'Auh ogtapn'\r\n&gt;&gt;&gt; text[2:13:2]\r\n'uh ogt'<\/pre>\n<p>In the above Python strings examples, it <strong>skips<\/strong> one character every time.<\/p>\n<h3>Deleting\/Updating Strings in Python<\/h3>\n<p>Strings are <strong>immutable<\/strong> and you cannot <strong>delete<\/strong> them or <strong>update<\/strong> them. You can assign a <strong>new object<\/strong> to the <strong>variable<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; work='RA'\r\n&gt;&gt;&gt; work='SRA'<\/pre>\n<p>This <strong>did not<\/strong> update the string. And you can delete a <strong>complete string<\/strong> with the <strong>del keyword<\/strong>, but you <strong>cannot delete<\/strong> a <strong>character<\/strong> or <strong>slice<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; del work\r\n&gt;&gt;&gt; del text[3]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<b>\u00a0\u00a0<\/b>File &#8220;&lt;pyshell#25&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<b>\u00a0\u00a0\u00a0\u00a0<\/b>del text[3]<br \/>\nTypeError: &#8216;str&#8217; object doesn&#8217;t support item deletion<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; del text[3:]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<b>\u00a0\u00a0<\/b>File &#8220;&lt;pyshell#26&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<b>\u00a0\u00a0\u00a0\u00a0<\/b>del text[3:]<br \/>\nTypeError: &#8216;str&#8217; object does not support item deletion<\/div>\n<h3>Concatenation and Membership in Python<\/h3>\n<h4>1. Concatenation in Python<\/h4>\n<p>You can <strong>join<\/strong> two strings with the <strong>+ operator<\/strong>. This is called <strong>concatenation<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'come'+' '+'home'<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;come home&#8217;<\/div>\n<p>You can also <strong>multiply<\/strong> a string by an integer.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'come'*3<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;comecomecome&#8217;<\/div>\n<p>But you <strong>cannot concatenate<\/strong> a string with an integer.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'help'+7<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<b>\u00a0\u00a0<\/b>File &#8220;&lt;pyshell#29&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<b>\u00a0\u00a0\u00a0\u00a0<\/b>&#8216;help&#8217;+7<br \/>\nTypeError: can only concatenate str (not &#8220;int&#8221;) to str<\/div>\n<h4>2. Membership in Python<\/h4>\n<p>You can <strong>check<\/strong> whether a string is <strong>present<\/strong> in another. You can do this with the <strong>\u2018in\u2019<\/strong> and <strong>\u2018not in\u2019<\/strong> operators.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'wire' in 'weird'<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'cool' not in 'honesty'<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h3>Python String Length<\/h3>\n<p>To calculate the length of a string, you have the <strong>len() in-built function<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; len(text)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">20<\/div>\n<p>You can use this in a <strong>for-loop<\/strong> to <strong>traverse<\/strong> on a <strong>collection<\/strong> or you can use it for some other reasons. Python also has other in-built functions and methods.<\/p>\n<p>We will talk about them later.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for index in range(len(text)):\r\n  print(index, text[index])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">0 A<br \/>\n1 y<br \/>\n2 u<br \/>\n3 s<br \/>\n4 h<br \/>\n5 i<br \/>\n6<br \/>\n7 b<br \/>\n8 o<br \/>\n9 u<br \/>\n10 g<br \/>\n11 h<br \/>\n12 t<br \/>\n13<br \/>\n14 a<br \/>\n15<br \/>\n16 p<br \/>\n17 e<br \/>\n18 n<br \/>\n19 !<\/div>\n<h3>String Methods and Functions in Python<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-string-methods-and-functions.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74794 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/12\/python-string-methods-and-functions.jpg\" alt=\"python string methods and functions\" width=\"762\" height=\"414\" \/><\/a><\/h3>\n<p>Python has many <strong>in-built<\/strong> methods and <strong>functions<\/strong> for strings. Today, in this Python strings article, we will discuss the most important ones.<\/p>\n<h4>1. strip() in Python<\/h4>\n<p>The <strong>strip()<\/strong> method <strong>removes<\/strong> <strong>whitespace<\/strong> around the string and returns the rest.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; ' hello '.strip()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;hello&#8217;<\/div>\n<p>You can also use <strong>lstrip()<\/strong> and <strong>rstrip()<\/strong> to remove the whitespace from only the<strong> left or the right<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; ' hello '.lstrip()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;hello &#8216;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; ' hello '.rstrip()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216; hello&#8217;<\/div>\n<h4>2. lower() and upper() in Python<\/h4>\n<p>We can convert a string to <strong>uppercase<\/strong> or <strong>lowercase<\/strong> using these methods. They do not change the <strong>original<\/strong> string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; text\r\n'Ayushi bought a pen!'\r\n&gt;&gt;&gt; text.upper()\r\n'AYUSHI BOUGHT A PEN!'\r\n&gt;&gt;&gt; text\r\n'Ayushi bought a pen!'\r\n&gt;&gt;&gt; text.lower()\r\n'ayushi bought a pen!'<\/pre>\n<h4>3. split() in Python<\/h4>\n<p>We can <strong>split<\/strong> a string around a <strong>separator<\/strong>. Let\u2019s split the \u2018text\u2019 string around spaces.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; text.split(' ')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[&#8216;Ayushi&#8217;, &#8216;bought&#8217;, &#8216;a&#8217;, &#8216;pen!&#8217;]<\/div>\n<h4>4. replace() in Python<\/h4>\n<p>We can also replace part of a string with <strong>another text<\/strong>. This is done using the<strong> replace() method<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; text.replace('bought','buys')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;Ayushi buys a pen!&#8217;<\/div>\n<h4>5. isdigit(), isalpha(), isalnum() in Python<\/h4>\n<p>We can use these methods to check whether a string is completely made of <strong>digits<\/strong>, <strong>alphabetical characters<\/strong> or <strong>both<\/strong>.<\/p>\n<h4>6. find() in Python<\/h4>\n<p>The <strong>find() <\/strong>method <strong>returns<\/strong> the first <strong>index<\/strong> where it finds a <strong>character<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; text.find('u')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<p>If it does not find it, it returns<strong> -1<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; text.find('z')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">-1<\/div>\n<h4>7. Python join()<\/h4>\n<p>We can join values from an <strong>iterable<\/strong> using a <strong>specific character<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; ','.join(['1','2','3'])<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;1,2,3&#8217;<\/div>\n<h4>8. startswith() and endswith() in Python<\/h4>\n<p>We can check whether a string <strong>starts<\/strong> with or <strong>ends<\/strong> with<strong> [endswith()]<\/strong> another string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; text.startswith('Ayu')<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>9. max() and min() in Python<\/h4>\n<p>The <strong>max()<\/strong> and <strong>min()<\/strong> methods give us the characters with the <strong>highest<\/strong> and <strong>lowest<\/strong> values.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; max(text)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;y&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; min(text)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216; &#8216;<\/div>\n<h3>Formatting Strings in Python<\/h3>\n<p>Strings are <strong>sequences<\/strong> of characters but can also <strong>hold<\/strong> values. We can format a string and <strong>embed<\/strong> values in it.<\/p>\n<p>There are 3 ways to do this in Python 3:<\/p>\n<ul>\n<li>format() method<\/li>\n<li>f-strings<\/li>\n<li>% operator<\/li>\n<\/ul>\n<h4>1. format() method in Python<\/h4>\n<p>We can use the format() method to decide what variables to use to <strong>embed<\/strong> <strong>values<\/strong> in the string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; cats=10\r\n&gt;&gt;&gt; 'She has {} cats'.format(cats)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;She has 10 cats&#8217;<\/div>\n<p>Here, it puts the value of cats in the <strong>curly braces<\/strong> part.<\/p>\n<h4>2. f-strings in Python<\/h4>\n<p><strong>Python 2<\/strong> does not have f-strings, but <strong>Python<\/strong> <strong>3.6+<\/strong> has them. In this, you <strong>prefix<\/strong> the string with an<strong> \u2018f\u2019<\/strong> and put variables in <strong>curly braces<\/strong> in the string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; f'She has {cats} cats'<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;She has 10 cats&#8217;<\/div>\n<h4>3. % operator in Python<\/h4>\n<p>We can also use the<strong> % operator<\/strong> to define <strong>values<\/strong>, <strong>position<\/strong> and <strong>type<\/strong> in the string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'She has %d cats' %(cats)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;She has 10 cats&#8217;<\/div>\n<h3>Summary<\/h3>\n<p>In this blog, we learned about strings in Python and how to <strong>use<\/strong>, <strong>define<\/strong> and <strong>alter<\/strong> them.<\/p>\n<p>We learned <strong>indexing<\/strong>, <strong>slicing<\/strong>, <strong>deleting<\/strong>, <strong>concatenating<\/strong> and <strong>calculating length<\/strong>, <strong>methods<\/strong> and <strong>functions<\/strong> and <strong>formatting strings<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will delve into Python strings, which are sequences of characters similar to character arrays in C++. We&#8217;ll cover the fundamentals of declaring strings, indexing and slicing them to access and&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":74795,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1154,1155,1156,1157,1158,1159,1160,1161],"class_list":["post-74652","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-formatting-strings-in-python","tag-python-slice-string","tag-python-string-length","tag-python-strings","tag-slicing-strings","tag-slicing-strings-in-python","tag-string-methods-and-functions-in-python","tag-strings-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Strings - Get ready to work with sequence of characters - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python Strings - Learn what are strings &amp; how to declare strings in Python. Also, see how to index, slice, delete, update and format strings.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-strings\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Strings - Get ready to work with sequence of characters - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python Strings - Learn what are strings &amp; how to declare strings in Python. Also, see how to index, slice, delete, update and format strings.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-strings\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-31T06:17:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-22T12:32:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-strings.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Strings - Get ready to work with sequence of characters - TechVidvan","description":"Python Strings - Learn what are strings & how to declare strings in Python. Also, see how to index, slice, delete, update and format strings.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-strings\/","og_locale":"en_US","og_type":"article","og_title":"Python Strings - Get ready to work with sequence of characters - TechVidvan","og_description":"Python Strings - Learn what are strings & how to declare strings in Python. Also, see how to index, slice, delete, update and format strings.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-strings\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2019-12-31T06:17:23+00:00","article_modified_time":"2024-08-22T12:32:37+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-strings.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-strings\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-strings\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Strings &#8211; Get ready to work with sequence of characters","datePublished":"2019-12-31T06:17:23+00:00","dateModified":"2024-08-22T12:32:37+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-strings\/"},"wordCount":1315,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-strings\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-strings.jpg","keywords":["Formatting Strings in python","Python Slice String","python string length","python strings","slicing strings","Slicing Strings in Python","String Methods and Functions in Python","Strings in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-strings\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-strings\/","url":"https:\/\/techvidvan.com\/tutorials\/python-strings\/","name":"Python Strings - Get ready to work with sequence of characters - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-strings\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-strings\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-strings.jpg","datePublished":"2019-12-31T06:17:23+00:00","dateModified":"2024-08-22T12:32:37+00:00","description":"Python Strings - Learn what are strings & how to declare strings in Python. Also, see how to index, slice, delete, update and format strings.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-strings\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-strings\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-strings\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-strings.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/12\/python-strings.jpg","width":802,"height":420,"caption":"strings in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-strings\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Strings &#8211; Get ready to work with sequence of characters"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=74652"}],"version-history":[{"count":2,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74652\/revisions"}],"predecessor-version":[{"id":447678,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/74652\/revisions\/447678"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/74795"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=74652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=74652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=74652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}