0

I've just started learning to code Python today on Grok Learning and I'm currently stuck on this problem. I have to create a code that reads a message and:

  • read the words in reverse order
  • only take the words in the message that start with an uppercase letter
  • make everything lowercase

I've done everything right but I can't get rid of a space at the end. I was wondering if anyone knew how to remove it. Here is my code:

code = [] 
translation = [] 

msg = input("code: ") 
code = msg.split()
code.reverse()

for c in code:
  if c[0].isupper(): 
    translation.append(c)  

print("says: ", end='')
for c in translation:     
    c = c.lower()
    print(c, end = ' ')

Thank you :)

meow
  • 11
  • 2
  • 1
    Does this answer your question? [Print a list of space-separated elements](https://stackoverflow.com/questions/22556449/print-a-list-of-space-separated-elements) – Tomerikoo Jun 11 '21 at 11:53

3 Answers3

0

You need to iterate for all of the letters in translation but the last and print it separately without the space:

for c in translation[:-1]:     
    c = c.lower()
    print(c, end = ' ')

print(translation[-1], end='')
sophros
  • 14,672
  • 11
  • 46
  • 75
  • Hi! Thanks this really helped, just needed to make the last part lowercase. However, when I submit my work, it says I'm missing a trailing newline character. I was wondering what it means and how to fix it? – meow Jun 11 '21 at 11:26
  • I don't know. I suggest that you remove `end=''` and check. – sophros Jun 11 '21 at 11:28
  • As a token of appreciation, could you please mark my answer as accepted (gray tick mark on the left of the answer) and upvote it if you like it? – sophros Jun 11 '21 at 11:28
0

This is a common problem:

  • You have a sequence of n elements
  • You want to format them in a string using a separator between the elements, resulting in n-1 separators

I'd say the pythonic way to do this, if you really want to build the resulting string, is str.join(). It takes any iterable, for example a list, of strings, and joins all the elements together using the string it was called on as a separator. Take this example:

my_list = ["1", "2", "3"]
joined = ", ".join(my_list)
# joined == "1, 2, 3"

In your case, you could do

msg = "Hello hello test Test asd adsa Das"
code = msg.split()
code.reverse()

translation = [c.lower() for c in code if c[0].isupper()]

print("says: ", end='')
print(" ".join(translation))
# output:
# says: das test hello

For printing, note that print can also take multiple elements and print them using a separator. So, you could use this:

print(*translation, sep=" ")

You could also leave out explicitly setting sep because a space is the default:

print(*translation)
He3lixxx
  • 3,263
  • 1
  • 12
  • 31
  • Hi! Thanks for the reply, but I haven't been introduced to .join() yet, so I'm not really sure what it means or does :) – meow Jun 11 '21 at 11:27
  • I've added a short explanation of what the method does. In case you didn't see that: I linked the python documentation of the method, which also has a nice, short explanation: "Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method." – He3lixxx Jun 11 '21 at 11:34
0

You can simply use join() and f-strings.

result = ' '.join(translation).lower()
print(f"says: {result}")
alec_djinn
  • 10,104
  • 8
  • 46
  • 71