3

For one of my tasks, the user inputs multiple strings until they enter a blank line and then it prints it out in one line, this is how I did it.

words = []
word = input("Word: ")

while word:
  words.append(word)
  word = input("Word: ")

words = (' ').join(words)
print(words)

However, there's another part where it takes the first letter of each string in the list and prints it out in capital letters in one line. I cannot figure out how to print this on one line. This is my code:

words_split = words.split() 
for word in words_split:
  i = word[0]
  print(i.upper())

e.g. If I entered ace, bravo, charlie: it would print out

A

C

E

instead of

ACE

Can someone assist me thank you (:

nbrooks
  • 18,126
  • 5
  • 54
  • 66
Nub coder
  • 49
  • 1
  • 1
  • 2

5 Answers5

3

I have another solution using Python List Comprehension i recomend read more about the method on the web.

if you have the next example:

words = 'Create a list of the first letters of every word in this string'

You can create a new list with every firts letter writing someting like that:

newlist = [x[0].upper() for x in words.split()]
print(newlist)

The correct output is:

 ['C', 'A', 'L', 'O', 'T', 'F', 'L', 'O', 'E', 'W', 'I', 'T', 'S']
1

Lots of possibilities, but this is how I would do it:

words_split = words.split() 
print(''.join(word[0].upper() for word in words_split))
user94559
  • 59,196
  • 6
  • 103
  • 103
0

Create an empty string outside of the for loop and add each letter to it inside the loop. Once the loop ends, print the string.

words_split = words.split()

output = ""

for word in words_split:
    output += word[0].upper()

print(output)

An alternate method is to change the terminating character of the print to nothing:

words_split = words.split()

for word in words_split:
    i = word[0]
    print(i.upper(), end='')
bgfvdu3w
  • 1,548
  • 1
  • 12
  • 17
  • So unindenting the print(output) makes it so it only prints the last result for the for loop? – Nub coder Aug 19 '17 at 06:18
  • It doesn't print the last result, but rather the final result of all the characters concatenated together. – bgfvdu3w Aug 19 '17 at 06:27
  • "Create an empty string outside of the for loop and add each letter to it inside the loop." Don't do that. It's very inefficient. Sometimes on later versions of Python the interpreter optimizes this, but many times it becomes O(n^2) – juanpa.arrivillaga Aug 19 '17 at 06:53
0

your problem is that the print function going one line down after each print, to solve it you can use the end parameter:

print("a",end = '')
print("b",end = '')

will print:

ab

instead of:

a

b

Community
  • 1
  • 1
Ely Manor
  • 11
  • 4
0

Updated my answer since @Smarx gave an identical one.

Considering:

words = "ABC BCA CAB"

This is how I'd put it:

first_letter_upper = [word[0].upper() for word in words.split()]
joined_words = ''.join(first_letter_upper)
print(joined_words)

Prints

ABC
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
  • You're missing a close paren on the `joined_words = ...` line. Also, no need to construct a list. (You can drop the square brackets.) – user94559 Aug 19 '17 at 06:17
  • @smarx A list get's constructed anyway, so passing a list is actually faster and no less memory efficient. – juanpa.arrivillaga Aug 19 '17 at 06:54
  • @smarx see [this answer](https://stackoverflow.com/a/9061024/5014455) and [this question](https://stackoverflow.com/questions/37782066/list-vs-generator-comprehension-speed-with-join-function) – juanpa.arrivillaga Aug 19 '17 at 06:55
  • @smarx saw you posted something similar/identical. Thanks for comments. I gave you +1 – Anton vBR Aug 19 '17 at 07:00
  • @juanpa.arrivillaga It seems to be correct that `str.join` needs a `list`, but I believe that in Python 3, it's neither faster nor more space efficient to construct the list yourself. – user94559 Aug 19 '17 at 07:00
  • @smarx hm, perhaps. I'd try to test it out, but I am currently on my phone. Athough, it is still currently the [basic example](https://docs.python.org/3.6/library/timeit.html) in the `timeit` docs. Either way. It's a micro-optimization that I generally don't worry about. – juanpa.arrivillaga Aug 19 '17 at 07:01