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.


+4 votes
asked in CSC201 Spring 2021 by (580 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 (8 points)
selected by

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.


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

return newString+'!'

or an f string:

return f'{newString}!'

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 (8 points)

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)):

answered by (8 points)