Strings Flashcards

1
Q

x = “,,,,,rrttgg…..banana….rrr”
return banana
using strip method

A

txt = “,,,,,rrttgg…..banana….rrr”
x = txt.strip(“,.grt”)
print(x)

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

x = a_string = ‘!hi. wh?at is the weat[h]er lik?e.’
return hi what is the weather like
using maketrans() method

A

import string

a_string = ‘!hi. wh?at is the weat[h]er lik?e.’
new_string = a_string.translate(str.maketrans(‘’, ‘’, string.punctuation))
print(new_string)

Returns: hi what is the weather like

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

‘Smart developers create their script in a text editor.’

Number of words

A

txt = ‘Smart developers create their script in a text editor.’

words = txt.split()

print(len(words))

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

Check if “free” is present in the following text:
txt = “The best things in life are free!”

A

print(“free” in txt)

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

Print only if “free” is present
txt = “The best things in life are free!”

A

if “free” in txt:
print(“Yes, ‘free’ is present.”)

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

Get the characters from the start to position 5 (not included):

b = “Hello, World!”

A

print(b[:5])

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

Get the characters from position 2, and all the way to the end:

b = “Hello, World!”

A

print(b[2:])

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

Get the characters:
From: “o” in “World!” (position -5)
To, but not included: “d” in “World!” (position -2):

b = “Hello, World!”

A

print(b[-5:-2])

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

The upper() method returns the string in upper case and lower () to return lower case.

A

a = “Hello, World!”

print(a.upper())
print(a.lower())

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

The strip() method removes any whitespace from the beginning or the end:
a = “ Hello, World! “

A

print(a.strip()) # returns “Hello, World!”

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

The replace() method replaces a string with another string:
a = “ Hello, World! “

A

print(a.replace(“H”, “J”))

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

The split() method splits the string into substrings if it finds instances of the separator:
a = “Hello, World!”
# returns [‘Hello’, ‘ World!’]

A

print(a.split(“,”))

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

quantity = 3
itemno = 567
price = 49.95
myorder = “I want {} pieces of item {} for {} dollars.”
Use format () method

A

print(myorder.format(quantity, itemno, price))

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

quantity = 3
itemno = 567
price = 49.95
myorder = ?????? What to put here?

print(myorder.format(quantity, itemno, price))

A

myorder = “I want to pay {2} dollars for {0} pieces of item {1}.”

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

Print the longest word and the number of its digit of a sentence:
‘Mondok egy tök hosszú mondatot, elfelejtettem a számot is’

A

txt=input(‘pleas provide a sentence: ‘)
szavak=txt.split
longest_word = max(szavak, key=len)
longest_word_length = len(longest_word)

print(f’A leghosszabb szó {longest_word} és {longest_word_length} karakterből áll. ‘)

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

Leghosszabb szó:
- Kérj be több szót a felhasználótól, metsd el a szavakat egy listába.
- Találd meg a leghosszabb szót és írd ki azt is hogy hány karakterből áll és hogy mi a szó.

A

ls = []

mondat = input(‘Mondj egy hosszú mondatot: ‘)

szavak = mondat.split()
ls.append(szavak)

longest_word = max(szavak, key=len)
longest_word_length = len(longest_word)

print(f’A leghosszabb szó {longest_word} és {longest_word_length} karakterből áll. ‘)

17
Q

my_string = ‘International TV Shows, TV Dramas, TV

Please print only until the first colon:
‘International TV Shows’

A

my_string = ‘International TV Shows, TV Dramas, TV Mysteries’
a = my_string.split(‘,’)[0]
a