#๐Ÿ”’ stuck on an exercise in learnpython.org about string operations

120 messages ยท Page 1 of 1 (latest)

steady turtle
#
s = "Hey thera! I em acer"
# Length should be 20
print("Length of s = %d" % len(s))

# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))

# Number of a's should be 2
print("a occurs %d times" % s.count("a"))

# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end

currently up to the "Slicing the string into bits" section (the last part), unsure what it is asking me. no instructions other than those hashtags, since it is an exercise to test my knowledge, but i got stuck for a few mins, so cam here for help. all help is appreciated.

heady pecanBOT
#

@steady turtle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

green ridge
#

it probably wants you to figure out what the output would be and then run the program to check your answers

steady turtle
#

huh

green ridge
#

that's a guess from me tho

#

further context would help

steady turtle
#
s = "Hey thera! I em acer"
# Length should be 20
print("Length of s = %d" % len(s))

# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))

# Number of a's should be 2
print("a occurs %d times" % s.count("a"))

# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end

# Convert everything to uppercase
print("String in uppercase: %s" % s.upper())

# Convert everything to lowercase
print("String in lowercase: %s" % s.lower())

# Check how a string starts
if s.startswith("Str"):
    print("String starts with 'Str'. Good!")

# Check how a string ends
if s.endswith("ome!"):
    print("String ends with 'ome!'. Good!")

# Split the string into three separate strings,
# each containing only a word
print("Split the words of the string: %s" % s.split(" "))

this is the code it gives and thats all the instructions i got, oh and btw the first two i think is what i did, which is why the s variable looks weird i had to match the requirements.

when i submitted this it said: Make sure you change the string assigned to s to match the exercise instructions

#

i dont get what it means by changing the string assigned to s

#

oh wait nvm

#

it means to follow everything else

#

i just dont get this part:

# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end
#

should i skip it...

#

๐Ÿค”

green ridge
#

well

#

I think when they say start to 5 they mean including 5

#

oh nvm first five chars

#

hmm

steady turtle
#

doesnt the index just go the 4 if it says 5

hoary abyss
green ridge
hoary abyss
steady turtle
steady turtle
hoary abyss
steady turtle
#

alright wait

hoary abyss
steady turtle
#

scroll down to the exercise

#

am i supposed to adjust the string each time?

hoary abyss
steady turtle
#

the others do?

#

i still have to change the string for the other lines

hoary abyss
#

the reason you don't understand what's being asked of you is that nothing is being asked of you. it's a strange exercise that way.

steady turtle
#

ohh

#

so you mean from line 11 and on i dont have to do anything?

hoary abyss
steady turtle
#

?

#

let me try agian

#

just checking index 8 in: "Hey thera! I em acer" is the a right before the exclamation mark?

#
s = "Hey thera! I em acer"
# Length should be 20
print("Length of s = %d" % len(s))

# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))

# Number of a's should be 2
print("a occurs %d times" % s.count("a"))

# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end

# Convert everything to uppercase
print("String in uppercase: %s" % s.upper())

# Convert everything to lowercase
print("String in lowercase: %s" % s.lower())

# Check how a string starts
if s.startswith("Str"):
    print("String starts with 'Str'. Good!")

# Check how a string ends
if s.endswith("ome!"):
    print("String ends with 'ome!'. Good!")

# Split the string into three separate strings,
# each containing only a word
print("Split the words of the string: %s" % s.split(" "))
#

thats what i submitted but it still said:

#

Make sure you change the string assigned to s to match the exercise instructions.

#

i did no?

hoary abyss
#

Ah, sorry, there's startwith and endswith that you need to account for.

#

and the last line, split()

steady turtle
#

?

#

what do i do?

#

could you do the exercise for me..

#

then i can understand why its not working?

#

Exercise
Try to fix the code to print out the correct information by changing the string.

s = "Hey there! what should this string be?"
# Length should be 20
print("Length of s = %d" % len(s))

# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))

# Number of a's should be 2
print("a occurs %d times" % s.count("a"))

# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end

# Convert everything to uppercase
print("String in uppercase: %s" % s.upper())

# Convert everything to lowercase
print("String in lowercase: %s" % s.lower())

# Check how a string starts
if s.startswith("Str"):
    print("String starts with 'Str'. Good!")

# Check how a string ends
if s.endswith("ome!"):
    print("String ends with 'ome!'. Good!")

# Split the string into three separate strings,
# each containing only a word
print("Split the words of the string: %s" % s.split(" "))
#

thats what it prompted me

gleaming mason
#

It seems to me from the question that they want you to devise a string which passes all the tests.

steady turtle
#

okay so what does that mean

#

so basically im just supposed to change the s string variable to match all the conditions i just dont know which conditions to follow they are all messed around and stuf

gleaming mason
#

Well, it looks like the last 3 conditions say that the string must start with Str and end with ome! and have exactly 3 words in it separated by spaces.

steady turtle
#

so im gonna change the start of 's' to Str and the end of 's' to ome!?

gleaming mason
#

I imagine so. In such a way that all the other tests pass - the first a at index 8, only 2 as, and so forth.

steady turtle
#

okay ill try and come back

#
s = "Strm d baeca of ome!"
# Length should be 20
print("Length of s = %d" % len(s))

# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))

# Number of a's should be 2
print("a occurs %d times" % s.count("a"))

# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end

# Convert everything to uppercase
print("String in uppercase: %s" % s.upper())

# Convert everything to lowercase
print("String in lowercase: %s" % s.lower())

# Check how a string starts
if s.startswith("Str"):
    print("String starts with 'Str'. Good!")

# Check how a string ends
if s.endswith("ome!"):
    print("String ends with 'ome!'. Good!")

# Split the string into three separate strings,
# each containing only a word
print("Split the words of the string: %s" % s.split(" "))
#

this is what i did and it worked but i think the split part at the bottom is what is wrong

gleaming mason
#

Well you have 5 words there. They want 3.

steady turtle
#

this output: script.py> output:
Length of s = 20
The first occurrence of the letter a = 8
a occurs 2 times
The first five characters are 'Strm '
The next five characters are 'd bae'
The thirteenth character is ' '
The characters with odd index are 'tmdbeao m!'
The last five characters are ' ome!'
String in uppercase: STRM D BAECA OF OME!
String in lowercase: strm d baeca of ome!
String starts with 'Str'. Good!
String ends with 'ome!'. Good!
Split the words of the string: ['Strm', 'd', 'baeca', 'of', 'ome!']

steady turtle
#
s = "Strmd   aaecaof ome!"
# Length should be 20
print("Length of s = %d" % len(s))

# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))

# Number of a's should be 2
print("a occurs %d times" % s.count("a"))

# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end

# Convert everything to uppercase
print("String in uppercase: %s" % s.upper())

# Convert everything to lowercase
print("String in lowercase: %s" % s.lower())

# Check how a string starts
if s.startswith("Str"):
    print("String starts with 'Str'. Good!")

# Check how a string ends
if s.endswith("ome!"):
    print("String ends with 'ome!'. Good!")

# Split the string into three separate strings,
# each containing only a word
print("Split the words of the string: %s" % s.split(" "))
#

still says:

#

Split the words of the string: ['Strmd', '', '', 'aaecaof', 'ome!']

gleaming mason
#

!doc str.split

The str.split method is a bit unusual.

See if you can figure out what's going on.

heady pecanBOT
#

str.split(sep=None, maxsplit=-1)```
Return a list of the words in the string, using *sep* as the delimiter string. If *maxsplit* is given, at most *maxsplit* splits are done (thus, the list will have at most `maxsplit+1` elements). If *maxsplit* is not specified or `-1`, then there is no limit on the number of splits (all possible splits are made).

If *sep* is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, `'1,,2'.split(',')` returns `['1', '', '2']`). The *sep* argument may consist of multiple characters as a single delimiter (to split with multiple delimiters, use [`re.split()`](https://docs.python.org/3/library/re.html#re.split)). Splitting an empty string with a specified separator returns `['']`.

For example:
steady turtle
#

wait what do i do?

gleaming mason
#

Well, how many "words" did you get from s.split(" ") ?

steady turtle
#

i never learnt whatever str.split is

gleaming mason
#

Hence the link to the docs above.

steady turtle
#

3?

#

isn it?

gleaming mason
#

Your output said:

 ['Strmd', '', '', 'aaecaof', 'ome!']

That looks like a list with 5 strings.

steady turtle
#

oh

#

then how do i fix it

#

without the spaces?

#

wait wait

gleaming mason
#

Readthe doc for str.split, shown above. Decide what it's doing to your string. Modify the string to get one 3 strings in the result.

steady turtle
#

i might of got it gimme a sec

dense mason
gleaming mason
steady turtle
#

yeah i like using f strings more

gleaming mason
#

Besides, f-strings are very new.

steady turtle
#

its alot easier

gleaming mason
#

For many things it is easier.

steady turtle
#

im tempted to skip this im really not getting what it is teaching me

hoary abyss
#

i would say that %-formatting is nearly obsolete, and f-strings are much better

steady turtle
#

exactly

gleaming mason
#

It's meant to get you to exercise your knowledge of strings and indexing.

steady turtle
#

python just making things complicated with % formatting

#

okay

gleaming mason
#

Really: reading docs is an essential programming skill. Read the str.split doc above carefully. Particularly the second paragraph. That should tell you exactly what's going on, why you got 5 strings, and therefore what you might change to get 3 strings.

steady turtle
#

i literally did all the requirements look

#
Length of s = 20
    The first occurrence of the letter a = 8
    a occurs 2 times
    The first five characters are 'Strhm'
    The next five characters are 'dh ab'
    The thirteenth character is 'a'
    The characters with odd index are 'thd bco m!'
    The last five characters are ' ome!'
    String in uppercase: STRHMDH ABECAOF OME!
    String in lowercase: strhmdh abecaof ome!
    String starts with 'Str'. Good!
    String ends with 'ome!'. Good!
    Split the words of the string: ['Strhmdh', 'abecaof', 'ome!']
#

its 3 strings is it not???

gleaming mason
#

Yes, looks good now.

steady turtle
#

and everything else is right?

#

then whats wrong with it

#

this error came up:

#

Make sure you change the string assigned to s to match the exercise instructions.

dense mason
steady turtle
#

๐Ÿ˜ญ

gleaming mason
#

Does the site still complain about your output above?

steady turtle
#

yes for some reason

#

maybe its time to skip it

#

even though i did it right?

gleaming mason
#

If you understand why str.spkit was causing you trouble, I'd say you're good.

One can spend forever trying to pass an opaue test where they don't tell you what they don't like.

steady turtle
#

yeah i do its because there was like spaces in between the words in the string which counted as an index

#

and for it to be 3 strings it had to be just one space in between each word

#

right?

gleaming mason
#

Yes. By contrast, s.split() would have given you 3 strings the first time - it splits of arbitrary runs of whitespace (spaces, tabs, newlines).

#

That's probably why they gave you " " explicitly as the separator. You need to consut the docs to see why things behave surprisingly.

steady turtle
#

ill go ahead and skip this exercise i think i learnt alot from that topic anyway, thanks guys ill close this help forum now

#

!close

heady pecanBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.