1

This is the code:

#swap capitalizations 
def swap_cases(word):
    for char in word:
        if char.upper() == char:
            b = char.lower()
            print(b)
        else:
            c = char.upper()
            print(c)

This is it's output:

h
I
i

I want it to appear like:

hIi
  • 1
    Does this answer your question? [Python: avoid new line with print command](https://stackoverflow.com/questions/11266068/python-avoid-new-line-with-print-command) – Tyler Stoney Oct 08 '20 at 14:52
  • `print` has a number of arguments, one is its `end` `print(...,end='')` another way is to hold your variables in a container and print them and use the `sep` argument and set it to `=''` – Umar.H Oct 08 '20 at 14:52

3 Answers3

0

just try this:

print(something, end='')
ZeFeng Zhu
  • 124
  • 1
  • 5
0

By adding end = "" parameter to print it won't go next line like below :

def swap_cases(word):
    for char in word:
        if char.upper() == char:
            b = char.lower()
            print(b,end = "")
        else:
            c = char.upper()
            print(c,end = "")

Hope it helps <3

Hosseinreza
  • 561
  • 7
  • 18
0

This will print '' as ending char for chars in a middle of the word, and '\n' for the last char.

def swap_cases(word):
    for i, char in enumerate(word):
        end = '\n' if i == (len(word) - 1) else ''
        if char.upper() == char:
            b = char.lower()
            print(b, end=end)
            else:
                c = char.upper()
                print(c, end=end)

so, you will get:

'hIi\n' instead of just 'hIi'