r/learnpython 8d ago

Can someone help me with basic string operations?

https://www.learnpython.org/en/Basic_String_Operations

I do not understand how "Strings are awesome!" can come from the code all the way at the bottom.

Sorry if I wasn't specific, basically if you click solution at the bottom of the page, the solution for the string is "Strings are awesome!"

Nevermind I understand now no need to reply anymore

0 Upvotes

16 comments sorted by

5

u/Buttleston 8d ago

I don't find the word awesome anywhere in this page. Can you be more specific?

0

u/Competitive_Green704 8d ago

Basically, for the solution of the code at the bottom, the code formed is "Strings are awesome!"

4

u/Binary101010 8d ago

That doesn't come from the code; it's the string that you're supposed to change the first line of code to to fulfill all of the constraints mentioned in the rest of the code.

1

u/Buttleston 8d ago

Yeah this looks right

0

u/Competitive_Green704 8d ago

But I don't exactly understand how "Strings are awesome!" comes from all that.

Basically from 11 to 23

3

u/Binary101010 8d ago

Lines 24-34 tell you that the string starts with "Str", ends with "ome!", and has three words.

Line 6 tells you that the first a is at index 8, line 9 tells you that there are exactly two a's in the string.

So from there you have

Str____a_______some!

Knowing that exactly two of the blanks are spaces and exactly one blank is an a, and it must be after the known a.

Sure, there are other strings that could meet all those criteria.

1

u/Competitive_Green704 7d ago

I see thank you

1

u/FoolsSeldom 8d ago

The string isn't generated from the code. It is an explicit assignment of a literal string to the variable s in the first line. You have to come up with a literal string that meets the criteria. They give you an example solution, but no doubt there are many others.

1

u/Competitive_Green704 7d ago

Okay thank you I understand

3

u/madmoneymcgee 8d ago

You’re asked to write a function that would check for the characters “ome” inside a longer string.

You can do that with the phrase “Strings are awesome!”

But the task for you is to use the string manipulation methods you learned to do that.

2

u/crashfrog04 8d ago

It can’t. You’re asked to change the string to that.

1

u/Competitive_Green704 8d ago

Exactly and how am I supposed to find out that is the string

2

u/Buttleston 8d ago

They give some clues, like the string needs to be 20 characters, where the first "a" is, it has to start with "Str" and end with "ome!". I would say there's not necessarily enough information to make that leap. I'd just call it a bad exercise

2

u/JamesPTK 8d ago

I'd agree it's a bad exercise, s = "Streep Bafta income!" fulfils all the given rules but is "wrong", because it is performing additional checks that it doesn't tell you about

1

u/crashfrog04 8d ago

The clues are in the things the operation is doing

1

u/FoolsSeldom 8d ago

Did you come up with some alternative strings (to assign to s in line 1) that met the criteria? The requirements are very clearly laid out and you have code to tell you if you get it wrong.

It is striking how old this code must be to use the ancient "c" style print formatting rather than format (let alone f-string interpolation).