

{"id":5410,"date":"2017-12-25T05:17:17","date_gmt":"2017-12-25T05:17:17","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=5410"},"modified":"2026-04-28T10:59:59","modified_gmt":"2026-04-28T05:29:59","slug":"python-string","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-string\/","title":{"rendered":"Python String &#8211; String Functions and Operations in Python"},"content":{"rendered":"<p>In this Python String tutorial, you will learn what is <span style=\"font-size: 16px\">Python string with examples. <\/span><\/p>\n<p><span style=\"font-size: 16px\">Moreover, you will learn how to declare and slice a string in python and also look at the Python String functions and Python String operations. <\/span><\/p>\n<p><span style=\"font-size: 16px\">At last, you will learn escape sequences in Python.\u00a0<\/span><\/p>\n<p>So, let&#8217;s start the Python String Tutorial.<\/p>\n<div id=\"attachment_42115\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-42115\" class=\"size-full wp-image-42115\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2.jpg\" alt=\"Python Strings\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-42115\" class=\"wp-caption-text\">Python Strings Tutorial &#8211; Functions and Operations<\/p><\/div>\n<h3>What is a Python String?<\/h3>\n<p>A string is text enclosed in quotes. Single (&#8216; &#8216;), double (&#8221; &#8220;), or triple quotes (&#8221;&#8217; &#8221;&#8217; \/ &#8220;&#8221;&#8221; &#8220;&#8221;&#8221;) all work. Strings let you store names, sentences, or even whole pages of text.<\/p>\n<p><strong>Characteristics of strings in Python:<\/strong><\/p>\n<ul>\n<li><strong>Immutable:<\/strong> Strings cannot be changed once it is made; changes can create a new string.<\/li>\n<li><strong>Sequence-based:<\/strong> You can access characters one by one by using index numbers.<\/li>\n<li><strong>Unicode support:<\/strong> Python strings can store letters, symbols, and emojis from any language.<\/li>\n<li><strong>No character type:<\/strong> Python does not have a separate character data type; even a single letter is considered a string in Python.<\/li>\n<\/ul>\n<p>You can prove this with the type() function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; type('Dogs are love')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;str&#8217;&gt;<\/div>\n<p>Python doesn\u2019t have the char data-type like C++ or Java does.<\/p>\n<h3>How to Declare a Python String?<\/h3>\n<p>You can declare a Python string using either single quotes or double quotes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='Dogs are love'\r\n&gt;&gt;&gt; print(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Dogs are love<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=\"Dogs are love\"\r\n&gt;&gt;&gt; print(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Dogs are love<\/div>\n<p>However, you cannot use a single quote to begin a string and a double quote to end it, and vice-versa.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='Dogs are love\"\r\n\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">SyntaxError: EOL while scanning string literal<\/div>\n<h3>How to Use Quotes inside Python String?<\/h3>\n<p>Since we delimit strings using quotes, there are some things you need to take care of when using them inside a string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=\"Dogs are \"love\"\"\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">SyntaxError: invalid syntax<\/div>\n<p>If you need to use double quotes inside a Python string, delimit the string with single quotes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='Dogs are \"love\"'\r\n&gt;&gt;&gt; print(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Dogs are &#8220;love&#8221;<\/div>\n<p>And if you need to use single quotes inside a string, delimit it with double-quotes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=\"Dogs are 'love'\"\r\n&gt;&gt;&gt; print(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Dogs are &#8216;love&#8217;<\/div>\n<p>You can use as many quotes as you want, then.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=\"'Dogs' 'are' 'love'\"\r\n&gt;&gt; print(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Dogs&#8217; &#8216;are&#8217; &#8216;love&#8217;<\/div>\n<h3>Spanning a String Across Lines<\/h3>\n<p>When you want to span a Python string across multiple lines, you can use triple quotes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">[php]&gt;&gt;&gt; a=\"\"\"Hello\r\n  Welcome\"\"\"\r\n&gt;&gt;&gt; print(a)[\/php]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Hello<br \/>\nWelcome<\/div>\n<p>It preserves newlines too, unlike using a backward slash for the same.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=\"Hello\\\r\n        Welcome\"\r\n&gt;&gt;&gt; print(a)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Hello\u00a0\u00a0\u00a0\u00a0\u00a0 Welcome<\/div>\n<h3>How to Access the Python String?<\/h3>\n<p>A string is immutable; it can\u2019t be changed.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=\"Dogs\"\r\n&gt;&gt;&gt; a[0]=\"H\"<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#22&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>a[0]=&#8221;H&#8221;<\/p>\n<p>TypeError: &#8216;str&#8217; object does not support item assignment<\/p>\n<\/div>\n<p>But you can access a string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=\"Dogs are love\"\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Dogs are love&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Dogs are love<\/div>\n<h4>1. Displaying a single character in Python<\/h4>\n<p>To display a single character from a string, put its index in square brackets. Indexing begins at 0.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a[1]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;o&#8217;<\/div>\n<h4>2. Slicing a string in Python<\/h4>\n<p>Sometimes, you may want to display only a part of a string. For this, use the slicing operator [].<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a[3:8]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;s are&#8217;<\/div>\n<p>Here, it printed characters 3 to 7, with the indexing beginning at 0.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a[:8]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Dogs are&#8217;<\/div>\n<p>This prints characters from the beginning to character 7.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a[8:]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216; love&#8217;<\/div>\n<p>This prints characters from character 8 to the end of the string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a[:]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Dogs are love&#8217;<\/div>\n<p>This prints the whole string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a[:-2]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Dogs are lo&#8217;<\/div>\n<p>This prints characters from the beginning to two characters less than the end of the string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a[-2:]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;ve&#8217;<\/div>\n<p>This prints characters from two characters from the end to the end of the string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a[-3:-2]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;o&#8217;<\/div>\n<p>This prints characters from three characters from the string\u2019s end to two characters from it.<\/p>\n<p>The following codes return empty strings.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a[-2:-2]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8221;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a[2:2]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8221;<\/div>\n<h3>Python String Concatenation<\/h3>\n<p>Concatenation is the operation of joining stuff together. Python Strings can be joined using the concatenation operator +.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='Do you see this, '\r\n&gt;&gt;&gt; b='$$?'\r\n&gt;&gt;&gt; a+b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Do you see this, $$?&#8217;<\/div>\n<p>Let\u2019s take another example.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='10'\r\n&gt;&gt;&gt; print(2*a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1010<\/div>\n<p>Multiplying \u2018a\u2019 by 2 returned 1010, and not 20, because \u201810\u2019 is a string, not a number. You cannot concatenate a string to a number.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; '10'+10<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#49&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>&#8217;10&#8217;+10<\/p>\n<p>TypeError: must be str, not int<\/p>\n<\/div>\n<h3>Python String Formatters<\/h3>\n<p>Sometimes, you may want to print variables along with a string. You can either use commas, or use string formatters for the same.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; city='Ahmedabad'\r\n&gt;&gt;&gt; print(\"Age\",21,\"City\",city)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Age 21 City Ahmedabad<\/div>\n<h4>1. f-strings in python<\/h4>\n<p>The letter \u2018f\u2019 precedes the string, and the variables are mentioned in curly braces in their places.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; name='Ayushi'\r\n&gt;&gt;&gt; print(f\"It isn't {name}'s birthday\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">It isn&#8217;t Ayushi&#8217;s birthday<\/div>\n<p>Notice that because we wanted to use two single quotes in the string, we delimited the entire string with double quotes instead.<\/p>\n<h4>2. format() method in python<\/h4>\n<p>You can use the format() method to do the same. It succeeds the string\u00a0and has the variables as arguments separated by commas.<br \/>\nIn the string, use curly braces to posit the variables. Inside the curly braces, you can either put 0,1,.. or the variables.<\/p>\n<p>When doing the latter, you must assign values to them in the format method.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(\"I love {0}\".format(a))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">I love dogs<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(\"I love {a}\".format(a='cats'))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">I love cats<\/div>\n<p>The variables don\u2019t have to defined before the print statement.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(\"I love {b}\".format(b='ferrets'))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">I love ferrets<\/div>\n<h4>3. % operator<\/h4>\n<p>The % operator goes where the variables go in a string. %s is for string<\/p>\n<p>What follows the string is the operator and variables in parentheses\/in a tuple.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; b='ferrets'\r\n&gt;&gt;&gt; print(\"I love %s and %s\" %(a,b))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">I love dogs and cats<\/div>\n<p>Other options include:<\/p>\n<p>%d \u2013 for integers<\/p>\n<p>%f \u2013 for floating-point numbers<\/p>\n<h3>Escape Sequences in Python<\/h3>\n<p>In a Python string, you may want to put a tab, a linefeed, or other such things. Escape sequences allow us to do this.<\/p>\n<p>An escape sequence is a backslash followed by a character, depending on what you want to do.<\/p>\n<p>Python supports the following sequences.<\/p>\n<ul>\n<li>\\n \u2013 linefeed<\/li>\n<li>\\t \u2013 tab<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(\"hell\\to\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">hell\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 o<\/div>\n<ul>\n<li>\\\\ &#8211; backslash<\/li>\n<\/ul>\n<p>Since a backslash may be a part of an escape sequence, so, a backslash must be escaped by a backslash too.<\/p>\n<ul>\n<li>\\\u2019 \u2013 A single quote can be escaped by a backslash. This lets you use single quotes freely in a string.<\/li>\n<li>\\\u201d \u2013 Like the single quote, the double quote can be escaped too.<\/li>\n<\/ul>\n<p>Any Doubt yet in Python String and Python String Operations and Functions? Please Comment.<\/p>\n<h3>Python String Functions<\/h3>\n<p>Python provides us with a number of functions that we can apply on strings or to create strings.<\/p>\n<h4>1. len() in python<\/h4>\n<p>The len() function in python returns the length of a string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='book'\r\n&gt;&gt;&gt; len(a)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<p>You can also use it to find how long a slice of the string is.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; len(a[2:])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<h4>2. str() in python<\/h4>\n<p>The str() function in Python converts any data type into a string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; str(2+3j)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;(2+3j)&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; str(['red','green','blue'])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8220;[&#8216;red&#8217;, &#8216;green&#8217;, &#8216;blue&#8217;]&#8221;<\/div>\n<h4>3. lower() and upper() in python<\/h4>\n<p>These methods return the string in lowercase and uppercase, respectively.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='Book'\r\n&gt;&gt;&gt; a.lower()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;book&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a.upper()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;BOOK&#8217;<\/div>\n<h4>4. strip() in python<\/h4>\n<p>The strip() function in Python removes whitespaces from the beginning and end of the string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='\u00a0 Book '\r\n&gt;&gt;&gt; a.strip()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Book&#8217;<\/div>\n<h4>5. isdigit() in python<\/h4>\n<p>The isdigit() function in Python returns True if all characters in a string are digits.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='777'\r\n&gt;&gt; a.isdigit()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='77a'\r\n&gt;&gt;&gt; a.isdigit()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h4>6. isalpha() in python<\/h4>\n<p>The isalpha() function in Python Returns True if all characters in a string are characters from an alphabet.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='abc'\r\n&gt;&gt;&gt; a.isalpha()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='ab7'\r\n&gt;&gt;&gt; a.isalpha()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h4>7. isspace() in python<\/h4>\n<p>The isspace() function in Python Returns True if all characters in a string are spaces.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='\u00a0\u00a0 '\r\n&gt;&gt;&gt; a.isspace()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=' \\'\u00a0 '\r\n&gt;&gt;&gt; a.isspace()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h4>8. startswith() in python<\/h4>\n<p>The startswith() function in Python takes a string as an argument, and returns True if the string it is applied on begins with the string in the argument.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a.startswith('un')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>9. endswith() in python<\/h4>\n<p>The endswith() function in Python takes a string as an argument, and returns True if the string it is applied on ends with the string in the argument.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='therefore'\r\n&gt;&gt;&gt; a.endswith('fore')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>10. find() in python<\/h4>\n<p>The find() function in Python takes\u00a0an argument and searches for it in the string on which it is applied.<\/p>\n<p>It then returns the index of the substring.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'homeowner'.find('meow')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<p>If the string doesn\u2019t exist in the main string, then the index it returns is 1.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'homeowner'.find('wow')<\/pre>\n<h4><strong>Output<\/strong><\/h4>\n<h4>-1<\/h4>\n<h4>11. replace() in python<\/h4>\n<p>The replace() function in Python takes two arguments. The first is the substring to be replaced. The second is the substring to replace with.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'banana'.replace('na','ha')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;bahaha&#8217;<\/div>\n<h4>12. split()<\/h4>\n<p>The split() function in Python takes one argument. The string is then split around every occurrence of the argument in the string.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'No. Okay. Why?'.split('.')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;No&#8217;, &#8216; Okay&#8217;, &#8216; Why?&#8217;]<\/div>\n<h4>13. join() in python<\/h4>\n<p>The join() function in Python takes a list as an argument and joins the elements in the list using the string it is applied on.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; \"*\".join(['red','green','blue'])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;red*green*blue&#8217;<\/div>\n<h3>Python String Operations<\/h3>\n<div id=\"attachment_35676\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-String-Operations-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-35676\" class=\"size-full wp-image-35676\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-String-Operations-01.jpg\" alt=\"Python String Operations\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-String-Operations-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-String-Operations-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-String-Operations-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-String-Operations-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-String-Operations-01-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-String-Operations-01-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-35676\" class=\"wp-caption-text\">Python String Operations<\/p><\/div>\n<h4>1. Comparison Operation in Python<\/h4>\n<p>Python Strings can compare using relational operators.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'hey'&lt;'hi'<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>\u2018hey\u2019 is lesser than \u2018hi lexicographically (because i comes after e in the dictionary)<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a='check'\r\n&gt;&gt;&gt; a=='check'<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'yes'!='no'<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>2. Arithmetic Operation in Python<\/h4>\n<p>Some arithmetic operations can be applied on strings.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'ba'+'na'*2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;banana&#8217;<\/div>\n<h4>3. Membership Operation in Python<\/h4>\n<p>The membership operators of Python can be used to check if string is a substring to another.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'na' in 'banana'<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'less' not in 'helpless'<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h4>4. Identity Operation in Python<\/h4>\n<p>Python\u2019s identity operators \u2018is\u2019 and \u2018is not\u2019 can be used on strings.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'Hey' is 'Hi'<\/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; 'Yo' is not 'yo'<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>5. Logical Operation in Python<\/h4>\n<p>Python\u2019s and, or, and not operators can be applied too. An empty string has a Boolean value of False.<\/p>\n<p><strong style=\"font-family: Verdana, Geneva, sans-serif\">a. and- <\/strong><span style=\"font-family: Verdana, Geneva, sans-serif;font-weight: inherit\">If the value on the left is True it returns the value on the right. <\/span><\/p>\n<p>Otherwise, the value on the left is False, it returns False.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; '' and '1'<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8221;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; '1' and ''<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8221;<\/div>\n<p><strong style=\"font-family: Verdana, Geneva, sans-serif\">b. or- <\/strong>If the value on the left is True, it returns True. Otherwise, the value on the right is returned.<br \/>\n<strong style=\"font-family: Verdana, Geneva, sans-serif\">c. not- <\/strong>As we said earlier, an empty string has a Boolean value of False.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; not('1')<\/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; not('')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>This was all about the tutorial on Python strings. Hope you like the Python strings tutorial.<\/p>\n<h3>Conclusion<\/h3>\n<p>In this Python String tutorial, you learned about Python string with string functions and Operators, and how to declare and access them.<\/p>\n<p>Then you learned about python string concatenation and formatters in python.<\/p>\n<p>Finally, You learned about Python string functions and operations that you can perform on strings.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Python String tutorial, you will learn what is Python string with examples. Moreover, you will learn how to declare and slice a string in python and also look at the Python String&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":42115,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[2858,10862,10863,13899,13927],"class_list":["post-5410","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-concatenate-strings-in-python","tag-python-string-functions","tag-python-string-operations","tag-string-formatters-in-python","tag-strings-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python String - String Functions and Operations in Python - DataFlair<\/title>\n<meta name=\"description\" content=\"A string is text enclosed in quotes. Single (&#039; &#039;), double (&quot; &quot;), or triple quotes (&#039;&#039;&#039; &#039;&#039;&#039; \/ &quot;&quot;&quot; &quot;&quot;&quot;) . Strings let you store names...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-string\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python String - String Functions and Operations in Python - DataFlair\" \/>\n<meta property=\"og:description\" content=\"A string is text enclosed in quotes. Single (&#039; &#039;), double (&quot; &quot;), or triple quotes (&#039;&#039;&#039; &#039;&#039;&#039; \/ &quot;&quot;&quot; &quot;&quot;&quot;) . Strings let you store names...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-string\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-12-25T05:17:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T05:29:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python String - String Functions and Operations in Python - DataFlair","description":"A string is text enclosed in quotes. Single (' '), double (\" \"), or triple quotes (''' ''' \/ \"\"\" \"\"\") . Strings let you store names...","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:\/\/data-flair.training\/blogs\/python-string\/","og_locale":"en_US","og_type":"article","og_title":"Python String - String Functions and Operations in Python - DataFlair","og_description":"A string is text enclosed in quotes. Single (' '), double (\" \"), or triple quotes (''' ''' \/ \"\"\" \"\"\") . Strings let you store names...","og_url":"https:\/\/data-flair.training\/blogs\/python-string\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2017-12-25T05:17:17+00:00","article_modified_time":"2026-04-28T05:29:59+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-string\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-string\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python String &#8211; String Functions and Operations in Python","datePublished":"2017-12-25T05:17:17+00:00","dateModified":"2026-04-28T05:29:59+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-string\/"},"wordCount":1647,"commentCount":10,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-string\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2.jpg","keywords":["concatenate strings in python","python string functions","python string operations","string formatters in python","Strings in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-string\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-string\/","url":"https:\/\/data-flair.training\/blogs\/python-string\/","name":"Python String - String Functions and Operations in Python - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-string\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-string\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2.jpg","datePublished":"2017-12-25T05:17:17+00:00","dateModified":"2026-04-28T05:29:59+00:00","description":"A string is text enclosed in quotes. Single (' '), double (\" \"), or triple quotes (''' ''' \/ \"\"\" \"\"\") . Strings let you store names...","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-string\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-string\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-string\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Strings-2.jpg","width":1200,"height":628,"caption":"Python Strings"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-string\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python String &#8211; String Functions and Operations in Python"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=5410"}],"version-history":[{"count":17,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5410\/revisions"}],"predecessor-version":[{"id":147972,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5410\/revisions\/147972"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/42115"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=5410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=5410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=5410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}