python substring

Posted by w3diy - 5 months ago

Python | Get the substring from given string using list slicing

ini_string = 'w3diydotcom'


print ("initial_strings : ", ini_string)


sstring_strt = ini_string[:2]
sstring_end = ini_string[3:]

print ("print resultant substring from start", sstring_strt)
print ("print resultant substring from end", sstring_end)

Example 2:

ini_string = 'w3diydotcom'

print ("initial_strings : ", ini_string)

sstring_alt = ini_string[::2]
sstring_gap2 = ini_string[::3]

print ("print resultant substring from start", sstring_alt)
print ("print resultant substring from end", sstring_gap2)

Example 3:

ini_string = 'w3diydotcom'

print ("initial_strings : ", ini_string)

sstring = ini_string[2:7:2]

# printing result
print ("print resultant substring", sstring)