Syntax Python Flashcards

(9 cards)

1
Q

How can you count the times a specific character shows up in a string

A

counter = string.count(char_of_intrest)

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

If wanting to use an if statement for when a character of interest is present in a string?

A

if char_of_interest in string:

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

Syntax when a character of interest is not present?

A

while strong not in (“characters of intrest “)

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

How to make it so that in a loop it checks for the characters in a string

A

for i in string:

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

syntax of the last item in a string ?

A

string[-1]

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

How to split a string?

A

line = input().split()
line=string.split()

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

How to split a list ?

A

1st = [“apple pie”, “chocolate bar”]
split_list = [item.split() for item in lst]
print(split_list)
# [[‘apple’, ‘pie’], [‘chocolate’, ‘bar’]]

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

how to get a random integer

A

random.randint()

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

if needing to break a phrases into looking for the presence of a specific variable

A

letter of interest input
phrases=input()

first- create a list of strings through splitting the phrases

words= phrases.split()

for word in words:
if letter in word:
print(f’found {letter} in {word}’)

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