FAQ: Code Challenge: String Methods - Count Multi X

This community-built FAQ covers the “Count Multi X” exercise from the lesson “Code Challenge: String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply () below!

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

A post was merged into an existing topic: How does python create an empty list?

13 posts were merged into an existing topic: How does `split()` work?

2 posts were split to a new topic: Printing number as string instead of int [solved]

30 posts were split to a new topic: How does python create an empty list?

3 posts were split to a new topic: Why doesn’t my code work? [solved]

3 posts were split to a new topic: Using slicing instead of a naive approach [solved]

how about this solution?

def count_multi_char_x(word, x):
splited = word.split(x)
joined = “”.join(splited)
return (len(word) - len(joined))/len(x)

5 posts were split to a new topic: Could I just use .count()?

That’s what I came up with too!

``````def count_multi_char_x(word, x):
count = word.split(x)
wordlen= ''.join(count)

return (len(word)-len(wordlen ))//len(x)
``````

can anyone validate this direction of logic? Is it on the right path or have I made a simple task more complex that it needs to be? Is there a more elegant way of doing it?

4 posts were split to a new topic: Can I iterate through the word? [solved]

7 posts were split to a new topic: Iterating through word search with steps? [solved]

2 posts were split to a new topic: Can I solve this with a built in method?

3 posts were split to a new topic: Can you review my code?

Creative!

What you do is actually removing the search string in the text (split by x then join with empty string). You could acheive same thing just by simple replace:

``````def count_multi_char_x(word, x):
replaced = word.replace(x, '')
return int((len(word) - len(replaced)) / len(x))

``````

( i added `int([...])` at return to fix `float` result format type

According to the solution code, when executing the first print comment(mississipi), how come the splits list get the elements of “m”, " ", “ipi”?
I thought it will get “m” and “ipi” only.

``````[spoiler]This text will be blurred[/spoiler]
# Write your count_multi_char_x function here:
def count_multi_char_x(word, x):
splits = word.split(x)
return(len(splits)-1)

# Uncomment these function calls to test your  function:
print(count_multi_char_x("mississippi", "iss"))
# should print 2
print(count_multi_char_x("apple", "pp"))
# should print 1[spoiler]This text will be blurred[/spoiler]
``````

Can someone explain exactly what this is saying?
split = word.split(x)
return(len(split)-1)

if you know what each thing there does then you can carry it out with pen and paper and observe what you do

or if not, then find out what that individual thing does before considering all of it at once. it’s not some impenetrable blob, there are individual parts that you need to learn before you can understand the overall thing

and, if there are individual parts in it that you can learn about, then maybe doing so lets you understand the overall thing and therefore don’t need to ask for help with it quite yet

I understand what each thing there does, but I’m confused on why we’re splitting at x. I also don’t know why we’re returning the length of the split and then putting the -1. I just started learning python and some of these things aren’t making sense, sorry for sounding a little stupid.

1 Like

how does this solution compare? (I’m impressed by the .join() one)

def count_multi_char_x(word, x):
n = range(len(word) - len(x) +1)
k =[ ]
for i in n:
if word[i] == x[0]:
k.append(word[i:i+len(x)])
return k.count(x)

I’ve basically isolated all stringlets of length = len(x) starting with x[0] and then counted the number of stringlets identical to x.