{"id":28170,"date":"2022-08-29T00:20:20","date_gmt":"2022-08-28T18:50:20","guid":{"rendered":"https:\/\/python-programs.com\/?p=28170"},"modified":"2022-08-29T00:20:20","modified_gmt":"2022-08-28T18:50:20","slug":"python-get-the-substring-from-given-string-using-list-slicing","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-get-the-substring-from-given-string-using-list-slicing\/","title":{"rendered":"How to Get the substring from given string using list slicing in Python?"},"content":{"rendered":"
A substring is a portion of a string. There are several methods in Python for creating a substring and checking if a substring exists in a string, the index of a substring, and more. Let’s take a look at some substring operations.<\/p>\n
Slicing <\/strong><\/p>\n You can extract a substring from a string by slicing with indices that correspond to your substring.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n A substring is a portion of a string. There are several methods in Python for creating a substring and checking if a substring exists in a string, the index of a substring, and more. Let’s take a look at some substring operations. Slicing You can extract a substring from a string by slicing with indices …<\/p>\ngvn_string[start:stop:step]<\/pre>\n
\n
Python Program to Get the substring from a given string using list slicing<\/h2>\n
Method #1: Slicing with start and stop values<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = 'hellothisispythonprograms'\r\n\r\n# Print the given string \r\nprint(\"The given string = \", gvn_str)\r\n\r\n# create a substring from the start of string till the required index you want \r\nsubstr_frm_start = gvn_str[:3]\r\n# create a substring from the end of string \r\nsubstr_frm_end = gvn_str[4:]\r\n\r\n# Print the substring from the starting of the string using slicing\r\n# here printing charcters from 0 to 2(excluding the last index) \r\nprint(\"The substring from the starting of the string = \", substr_frm_start)\r\n# Print the substring from the end of the string using slicing\r\n# here printing characters from 4 to entire string\r\nprint(\"The substring from the end of the string = \", substr_frm_end)<\/pre>\n
The given string = hellothisispythonprograms\r\nThe substring from the starting of the string = hel\r\nThe substring from the end of the string = othisispythonprograms<\/pre>\n
Method #2: Slicing with a step value<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = 'hellothisispythonprograms'\r\n# Print the given string \r\nprint(\"The given string = \", gvn_str)\r\n\r\n# Get the substring by taking step value as 3 to the slicing\r\nsubstr_withstep3 = gvn_str[::3]\r\n# Get the alternative character substring by taking step value as 2\r\nsubstr_withstep2 = gvn_str[::2]\r\n\r\n# print the substring from the start of the given string with 3 step\r\nprint(\"The substring from the start of the given string with 3 step = \", substr_withstep3)\r\n# print the substring from the given string with alternate characters(step=2)\r\nprint(\"The substring from the given string with alternate characters(step=2)= \", substr_withstep2)<\/pre>\n
The given string = hellothisispythonprograms\r\nThe substring from the start of the given string with 3 step = hlhiyorrs\r\nThe substring from the given string with alternate characters(step=2)= hlohssyhnrgas<\/pre>\n
Method #3: Slicing with start, stop and step values<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = 'hellothisispythonprograms'\r\n\r\n# Print the given string \r\nprint(\"The given string = \", gvn_str)\r\n\r\n# Slice from 2nd index to 7 index with alternate characters(because we used step value as 2)\r\nsub_string = gvn_str[2:7:2]\r\n\r\n# Print the result substring \r\nprint (\"The result substring = \", sub_string)\r\n<\/pre>\n
The given string = hellothisispythonprograms\r\nThe result substring = loh<\/pre>\n
Method #4: Slicing with start, stop and step values (User Input)<\/h3>\n
\n
\nand store it in another variable<\/li>\n# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Give some random string = \")\r\n\r\n# Print the given string \r\nprint(\"The given string = \", gvn_str)\r\n\r\n# Give the start value as user input using the int(), input() functions\r\n# and store it in another variable (here int() converts to an integer datatype)\r\ngvn_startval = int(input(\"Give some random start value = \"))\r\n# Give the end value as user input using the int(), input() functions\r\n# and store it in another variable \r\ngvn_endval = int(input(\"Give some random end value = \"))\r\n# Give the step value as user input using the int(), input() functions\r\n# and store it in another variable \r\ngvn_step = int(input(\"Give some random step value = \"))\r\n\r\n# Slice the given string based on the above given start, end and step values \r\n# and store it in another variable \r\nsub_string = gvn_str[gvn_startval:gvn_endval:gvn_step]\r\n\r\n# Print the result substring \r\nprint (\"The result substring = \", sub_string)<\/pre>\n
Give some random string = good morning python programs\r\nThe given string = good morning python programs\r\nGive some random start value = 1\r\nGive some random end value = 15\r\nGive some random step value = 2\r\nThe result substring = odmrigp<\/pre>\n
Here the string \"good morning python programs\" is sliced from the \r\nstart index=1(o) to the end index 15(t) with a step value =2 i.e, \r\nit prints alternative characters<\/pre>\n
Method #5: Slicing with a step value (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Give some random string = \")\r\n# Print the given string \r\nprint(\"The given string = \", gvn_str)\r\n\r\n# Give the step value as user input using the int(), input() functions\r\n# and store it in another variable (here int() function converts to an integer datatype)\r\ngvn_step = int(input(\"Give some random step value = \"))\r\n\r\n# Get the substring by taking the given step value to perform slicing\r\nsubstr_withstep = gvn_str[::gvn_step]\r\n\r\n# print the substring from the input string based on the given step value \r\nprint(\"The substring from the input string based on the given step value = \", substr_withstep)\r\n<\/pre>\n
Give some random string = hellothisispythonprograms\r\nThe given string = hellothisispythonprograms\r\nGive some random step value = 2\r\nThe substring from the input string based on the given step value = hlohssyhnrgas<\/pre>\n","protected":false},"excerpt":{"rendered":"