String Indexing / slicing

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/10

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

11 Terms

1
New cards

What is indexing? Explain it and how it’s written and alternatives

Indexing = Allowing us to access elements of a sequence with [ ] (indexing operator) 

[start : end : step]

[1]

[:2:]

[1::]

[::9]

2
New cards

Find the first number in Credit_number = “1234 - 5687- 90123 - 4567”

print(Credit_number [0])

returns 1

3
New cards

Write to find first four digits of Credit_number = “1234 - 5687- 90123 - 4567”

print(Credit_number [0:4])

  • This gives us start and stop, so first four elements 

  • This will have us start start at one, and end 4

    • Indexing is exclusive, so if I see 4, it won't include that one. It will go up to 3)

  • I can also write this as (:4) because I don’t need the zero 

4
New cards

Write to find 5th to 9th digits of Credit_number = “1234 - 5687- 90123 - 4567”

print(Credit_number [5:9])

  • This will print 5678

    • (because dash counts as one)

5
New cards

Write to find all numbers up to 5th place of Credit_number = “1234 - 5687- 90123 - 4567”

print(Credit_number [:5])

  • This will print everything up to the 5th place of a string 

  • However, remeber that the first of these 5 is labeled as zero.

  • Returns 1234-

6
New cards

Explain the inclusive / exclusive nature of indexing and slicing.

[start : end : step]

Start is inclusive

End is exclusive

if end is 5, it will count up to 4 places in the srring.

7
New cards

Write to find all numbers AFTER the 5th place of Credit_number = “1234 - 5687- 90123 - 4567”

print(Credit_number[5:]

5687- 90123 - 4567

8
New cards

Return the last number of the credit card number and explain why

print(Credit_number [-1])

9
New cards

Return the 3rd to last number of the credit card number and explain why

print(Credit_number [-3])

10
New cards

Write the credit card number backwards and explain why

print(Credit_number [::-1])

11
New cards

Write a script to show the last for like this: xxxx-xxxx-xxxx-4567

Credit_number = “1234 - 5687- 90123 - 4567”

Credit_number = Credit_number[-4:]

print(f”xxxx-xxxx-xxxx-{Credit_number}”)