Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+4 votes
asked in CSC201 Spring 2021 by (6k points)

3 Answers

+4 votes
 
Best answer

You can iterate over a string like so:

word = 'elnatan'
for letter in word:

Each time through the loop, it will look at 1 character of the string. You can use concatenation (+) to construct a new string with dots or dashes added. You will want to start with initializing an empty string before the loop;

newString = ""
answered by (6.8k points)
selected by
+1

How do I get the dot or dash at the beginning and end of the word? I did the concatenation like you suggest here, but I'm forgetting how to do that part.

+1

There are a couple ways. You could use more concatenation, like this:

return newString+'!'

or an f string:

return f'{newString}!'
+1

outside the loop, if your variable was a then do a=a+'-'. does that make sense?

+4 votes

As strings are immutable, my approach is to call each index of the string and use concatenation. I start by creating a blank str variable =’ ‘ variable and then use for loop to put each index, + ‘.’ in the variable that I create.

answered by (2.8k points)
0

FYI, the quote of the result automatically appears if the return function returns a string

+2 votes

You can try this method:
for num in range (0,len(word)):

        changed=changed+'-'+word[num]   
    changed=changed+'-'
    return(changed)
answered by (4.3k points)
...