ANSWERS TO HELP IN (Problem Solving
& Understanding) 100% VERIFIED .
What is indexing used for in Python?
"Indexing" is used when referring to an element of an inerrable by its position within
the inerrable. This is done through indexing [ ].
CQ: Try creating a substring based off of the first word or name in the sentence.
use the following variable:
name = "Johnny Appleseed"
Once finished.
Try creating a substring based off of the last word or name in the sentence.
Once finished.
Next, create a weird name from the variable by leaving the first and last name in
but making it so it skips every other letter.
Lastly, reverse the name or text of the value in the variable by making it read
In order to write a line of code that can slice a specific part of the name such as
"Johnny" by itself,
We'll use the following code:
first_name = name[0:6]
or
,first_name = name[:6]
Then...
print(first_name)
Now, in order to write a line of code that can slice the last word or name such as
"Applseed".
We'll use the following code:
last_name = name[7:17]
or
last_name = name[7:]
Then...
print(last_name)
Note: For slicing we'll use the colon to snip a part of the text in the variable by listing
the number the letter starts with which is always "0" and "6" since we'll always take
an extra character at the end or you can also see it as there's 6 letters in "Johnny" but
we still always start with zero.
Next, in order to create a weird name we'll need to add a third colon instead of just
the two we've been using previously.
We'll use the following code:
funny_name = name[0:16:2]
or
, funny_name = name[::2]
print(funny_name)
Note: As you can see the program will skip every other letter in "Johnny Appleseed".
"Johnny Appleseed"
"J h n p l s e "
Lastly, in order to create a reversed name we'll use negative numbers. Similar to first
puzzle just counting starting from the last letter instead of the first.
reversed_name=[-1:-17:-1]
or
reversed_name=[::-1]
print(reversed_name)
Note: When counting backwards we'll always count in the negatives. Due to counting
in the negatives there's no need to count zero since -0 isn't an actual number so we'll
default into -1 as the first letter reversed counting backwards in the last colon.
CQ: Try removing everything but the name of from the website link from the value
in the variable.
use the following variable:
website = "https://google.com"
In order to remove everything but the name of the website we'll use the slice option.
For "Slice" it's a little bit different so we'll use a comma instead of a colon for slice
unlike the indexing.