Answers for "python stride"

10

python string slicing

my_string = "Hey, This is a sample text"
print(my_string[2:]) #prints y, This is a sample text
print(my_string[2:7]) #prints y, Th excluding the last index
print(my_string[2::2]) #prints y hsi  apetx
print(my_string[::-1]) #reverses the string => txet elpmas a si sihT ,yeH
Posted by: Guest on May-03-2020
0

python stride

# The stride is the third (and optional) number in a slice.
# The default value is 1.

myString = "string"
# prints every second character, beginning with the 0th index
print(myString[::2])  # outputs "srn"
# prints every second character, beginning with the last index and going backwards
print(myString[::-2])  # outputs "git"

myList = [0, 1, 2, 3, 4, 5, 6, 7]
# prints every third character, beginning with the 0th index
print(myList[::3])  # outputs [0, 3, 6]

myTuple = ("a", "b", "c", "d", "e")
# prints every character
print(myTuple[::])  # outputs ("a", "b", "c", "d", "e")
Posted by: Guest on December-19-2020

Python Answers by Framework

Browse Popular Code Answers by Language