String Operations Flashcards

1
Q

Pull out the first digit in a string

A

stringname[0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Pull out the nth digit of a string

A

stringname[n]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Pull out the last digit of a string (using negative indexing)

A

stringname[-1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

find the length of a string

A

len(stringname)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

slice the first three digits of a string

A

stringname[0:3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

get every second letter of a string

A

stringname[::2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

get every second letter of a string starting from the 4th letter to the 8th

A

stringname[3:8:2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is

‘1’ + ‘2’

A

‘12’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how would i replicate a string three times

A

3 * “string goes in here”

OR

3 * stringname

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how do i make a new line in a printed string

A

\n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how to i add a tab in a printed string

A

\t

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how do i put a backslash in a printed string

A

put two \

OR

print(r”string goes \ in here”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

where do you put an r for raw string

A

just before the first “ of the string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is the general format for operations?

A

variable.operation()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

convert stringname to uppercase letters

A

newstringname = stringname.upper()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

convert stringname to lowercase letters

A

newstringname = stringname.lower()

17
Q

turn all instances of “michael” into “janet” in stringname - NOT RegEx

A

stringname.replace(“Michael”, “Janet”)

18
Q

how do you link repeated commands

A

stringname.function().function().function()

19
Q

write a command to figure out at what index number “Jack” starts in the string “Michael Jackson”

A

name = “Michael Jackson”
name.find(‘Jack’)

20
Q

what is output in a find command if the substring cannot be located in the string

A

-1

21
Q

what is the import to import Regular Expressions

A

import re

22
Q

how do you check whether there is a match for something in regular expression

A

result = re.search(pattern, stringname)

returns as NoneType or re.Match

23
Q

what would you use to match any digit in RegEx

A

\d

24
Q

what would match any non-word character in RegEx

A

\W

25
Q

what would match any white space character in RegEx

A

\s

26
Q

in RegEx, what is used to locate all of the occurrences of a specified pattern in a string

A

matches=re.findall(pattern, stringname)

outputs each individual instance in a list

27
Q

in RegEx, how would you separate a string into a list each time there was a space

A

list = re.split(“\s”, stringname)

28
Q

In RegEx, how would you replace all instances of “Tom” with “Mary”

A

output = re.sub(“Tom”, “Mary”, stringname)

29
Q

stringname.replace(“Matthew”, “Mary) is similar to what command in RegEx

A

re.sub(“Matthew”, “Mary”, stringname)

30
Q

how is search in RegEx different from find

A

find returns the index value for that substring (ie returns 9 for “jack” in “michael jackson”)

search tells you whether or not there is a match for such thing, and then you can use match.group() to display the match