{"id":35079,"date":"2022-12-20T13:51:52","date_gmt":"2022-12-20T13:51:52","guid":{"rendered":"https:\/\/naiveskill.com\/?p=35079"},"modified":"2022-12-22T12:23:36","modified_gmt":"2022-12-22T12:23:36","slug":"python-substring","status":"publish","type":"post","link":"https:\/\/naiveskill.com\/python-substring\/","title":{"rendered":"Python substring: A comprehensive tutorial with examples in 2023"},"content":{"rendered":"\n

In this tutorial, I’ll explain the working of python<\/a> substring. I will also explain the behavior of python substring with multiple examples. so let’s get started.<\/p>\n\n\n\n

Introduction to python substring<\/h2>\n\n\n\n
\"\"<\/figure>\n\n\n\n

A substring is a portion of the string. Python provides various ways to get a substring from a string. The most common is list slicing. I will explain other substring techniques as we proceed further.<\/p>\n\n\n\n

Syntax of python substring<\/h3>\n\n\n\n

The python list slicing works on index position. Below is the syntax of python substring index slicing.<\/p>\n\n\n\n

my_string[start:stop:step]\n\nwhere:\n    start: index of the list where slicing starts.\n    stop: index of the list where slicing ends.\n    step(optional): select the step within the range start to stop index.<\/code><\/pre>\n\n\n\n

Get substring from python string<\/h2>\n\n\n\n

Let’s assume our string is “naive_skill<\/strong>” and we wish to extract skill<\/strong> from it. Below is the code snipped for the same.<\/p>\n\n\n\n

l = \"naive_skill\"\nsubstr = l[6:11]\nprint(substr)<\/code><\/pre>\n\n\n\n

The above codes give skill<\/strong> as output. The starting index is 6 and the stop index is 11.<\/p>\n\n\n\n

\"Get<\/figure>\n\n\n\n

Python substring examples<\/h2>\n\n\n\n
\"\"<\/figure>\n\n\n\n

In this session, we will understand the working of python substring with multiple examples. <\/p>\n\n\n\n

Python substring after a specific character<\/h3>\n\n\n\n

let’s assume our string is “visit naive skill to learn awesome technologies”<\/strong> and we wish to get the substring appears after the skill.<\/p>\n\n\n\n

l = \"visit naive skill to learn awesome technologies\"\nl.split(\"skill\",1)[1]<\/code><\/pre>\n\n\n\n

we have to use the split function to split the data on the string “skill<\/strong>“.(“skill”,1) signifies spilling the data on 1st occurrence of the skill.<\/p>\n\n\n\n

\"Python<\/figure>\n\n\n\n

Python substring index<\/h3>\n\n\n\n

In this session, we will understand the python index method with the help of an example. The python index()<\/strong> method is used to get the index of a substring. The index method returns the lowest<\/strong> index in the string where the substring is found.<\/p>\n\n\n\n

suppose our string is “visit naive skill to learn technologies<\/strong>” and we want to know the index of “learn<\/strong>“.<\/p>\n\n\n\n

l = \"visit naive skill to learn technologies\"\nl.index(\"learn\")<\/code><\/pre>\n\n\n\n

since the lowest index was 21 so we get 21 as output.<\/p>\n\n\n\n

\"Python<\/figure>\n\n\n\n

Python string contains a substring<\/h3>\n\n\n\n

In some cases, a user needs to know whether a particular substring is present inside the string. This can be easily checked using the simple if condition.<\/p>\n\n\n\n

l = \"visit naive skill to learn technologies\"\nif \"skill\" in l:\n  print(\"found\")\nelse:\n  print(\"not found\")<\/code><\/pre>\n\n\n\n

Using the above code we can verify if the substring skill<\/strong> is present in string l.Since skill is present in l, so we get found<\/strong> as an output<\/p>\n\n\n\n

\"Python<\/figure>\n\n\n\n

Python substring extract a number<\/h3>\n\n\n\n

python provides the isdigit() <\/strong>function to check the presence of a digit inside a string. The isdigit() <\/strong>function returns true<\/strong> if the string contains an integer else returns false<\/strong>.<\/p>\n\n\n\n

str = \"naive4skill\"\ndigit=\"\"\nfor i in str:\n  if i.isdigit():\n    digit = digit + i\nprint(digit)<\/code><\/pre>\n\n\n\n

The above code returns 4 as the digit<\/p>\n\n\n\n

\"Python<\/figure>\n\n\n\n

Python substring regex<\/h3>\n\n\n\n

Python also provides the facility to extract the substring using regex. The regex should be used in places where substring can be extracted using the index method.<\/p>\n\n\n\n

To use the regex a user needs to import<\/strong> the re module. Let’s say we need to extract the first occurrence of digit\/digits from the string str<\/strong>.<\/p>\n\n\n\n

import re\nstr = \"789Naive456skill123\"\nm = re.search(r'\\d+', str)\nprint(m.group())<\/code><\/pre>\n\n\n\n

The above codes give 789 as output because this is the first number that appears in the string str<\/strong><\/p>\n\n\n\n

\"Python<\/figure>\n\n\n\n

If a user wishes to get all the occurrences of digit\/digits in string str then he has to use the findall()<\/strong> method.<\/p>\n\n\n\n

import re\nstr = \"789Naive456skill123\"\nre.findall(r'\\d+', str)<\/code><\/pre>\n\n\n\n

The above code returns all the numbers present in str.<\/p>\n\n\n\n

\"Python<\/figure>\n\n\n\n

<\/p>\n\n\n\n

Python substring between two characters<\/h3>\n\n\n\n

Using python programming, a user can also extract a string present between two substrings. Let’s say we need to get the string between two substrings s1 and s2.<\/p>\n\n\n\n

l = \"visit naive skill to learn awesome technologies\"\n\n## substrings\ns1 = \"skill\"\ns2 = \"technologies\"\n\n## get index of substring\nidx1 = l.index(s1)\nidx2 = l.index(s2)\n\n## getting elements in between\nstarting_index = idx1 + len(s1) + 1\nend_index = idx2\n\nstring = l[starting_index:end_index]\nprint(string)<\/code><\/pre>\n\n\n\n

The above code prints the string present between skill<\/strong> and technologies<\/strong> which is to learn awesome<\/strong>.<\/p>\n\n\n\n

\"Python<\/figure>\n\n\n\n

Conclusion<\/h2>\n\n\n\n

I hope you have liked this tutorial on python substring. Please reach out to me if you need further input.<\/p>\n\n\n\n

More to explore?<\/h2>\n\n\n\n

Python request post and get<\/a><\/p>\n\n\n\n

Python substring<\/a><\/p>\n\n\n\n

Python square root<\/a><\/p>\n\n\n\n

Python NULL detailed tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

In this tutorial, I’ll explain the working of python substring. I will also explain the behavior of python substring with multiple examples. so let’s get started. Introduction to python substring …<\/p>\n

Python substring: A comprehensive tutorial with examples in 2023<\/span> Read More \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":35123,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"","footnotes":""},"categories":[70,34],"tags":[74,49],"uagb_featured_image_src":{"full":["https:\/\/naiveskill.com\/wp-content\/uploads\/2022\/12\/Python-range-function-3.webp",1200,628,false],"thumbnail":["https:\/\/naiveskill.com\/wp-content\/uploads\/2022\/12\/Python-range-function-3-150x150.webp",150,150,true],"medium":["https:\/\/naiveskill.com\/wp-content\/uploads\/2022\/12\/Python-range-function-3-300x157.webp",300,157,true],"medium_large":["https:\/\/naiveskill.com\/wp-content\/uploads\/2022\/12\/Python-range-function-3-768x402.webp",768,402,true],"large":["https:\/\/naiveskill.com\/wp-content\/uploads\/2022\/12\/Python-range-function-3-1024x536.webp",1024,536,true],"1536x1536":["https:\/\/naiveskill.com\/wp-content\/uploads\/2022\/12\/Python-range-function-3.webp",1200,628,false],"2048x2048":["https:\/\/naiveskill.com\/wp-content\/uploads\/2022\/12\/Python-range-function-3.webp",1200,628,false],"web-stories-poster-portrait":["https:\/\/naiveskill.com\/wp-content\/uploads\/2022\/12\/Python-range-function-3-640x628.webp",640,628,true],"web-stories-publisher-logo":["https:\/\/naiveskill.com\/wp-content\/uploads\/2022\/12\/Python-range-function-3-96x96.webp",96,96,true],"web-stories-thumbnail":["https:\/\/naiveskill.com\/wp-content\/uploads\/2022\/12\/Python-range-function-3-150x79.webp",150,79,true]},"uagb_author_info":{"display_name":"naivetechblog@gmail.com","author_link":"https:\/\/naiveskill.com\/author\/naivetechbloggmail-com\/"},"uagb_comment_info":1,"uagb_excerpt":"In this tutorial, I’ll explain the working of python substring. I will also explain the behavior of python substring with multiple examples. so let’s get started. Introduction to python substring … Python substring: A comprehensive tutorial with examples in 2023 Read More \u00bb","_links":{"self":[{"href":"https:\/\/naiveskill.com\/wp-json\/wp\/v2\/posts\/35079"}],"collection":[{"href":"https:\/\/naiveskill.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/naiveskill.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/naiveskill.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/naiveskill.com\/wp-json\/wp\/v2\/comments?post=35079"}],"version-history":[{"count":0,"href":"https:\/\/naiveskill.com\/wp-json\/wp\/v2\/posts\/35079\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/naiveskill.com\/wp-json\/wp\/v2\/media\/35123"}],"wp:attachment":[{"href":"https:\/\/naiveskill.com\/wp-json\/wp\/v2\/media?parent=35079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/naiveskill.com\/wp-json\/wp\/v2\/categories?post=35079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/naiveskill.com\/wp-json\/wp\/v2\/tags?post=35079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}