Home > Enterprise >  Can someone explain to me why '\n' doesn't register when it is used in with a functi
Can someone explain to me why '\n' doesn't register when it is used in with a functi

Time:11-06

For context, this is the camel case #4 question from HackerRank.

I am given a multi-string such as test...

test = '''S;M;plasticCup()
C;V;mobile phone
C;C;coffee machine
'''

and I need to manipulate it based on several conditions (not relevant). Once I have successfully manipulated test, I need to return my own multi-line string output, which I am trying to accomplish below.

def feeder(user_input):
    split_total = user_input.splitlines()
    output = ''
    for element in split_total:
        output  = f"{camelCase(element)} \n"
        #camelCase()` is the user-defined function that applies the "several conditions" mentioned earlier
        #code snippet for camelCase() provided at bottom of the post, in case it matters
    return output
feeder(test)

However, my output variable completely ignores the newline \ns?

'plastic cup \nmobilePhone \nCoffeeMachine'

What gives?

I tried newline and it works here?

chr_list = ['a', 'b', 'c']
output = ''
for element in chr_list:
    output  = f"{element}\n"
print(output)

OUTPUT

a
b
c

CamelCase code

def camelCase(user_input):
    split_ls = user_input.split(';')
    if (split_ls[0] == 'S'): #splitting
        index_of_upr, split_of_upr = [], []
        split_ls[2] = split_ls[2].replace('()', '') #remove method's ()
        for index, character in enumerate(split_ls[2]):
            if (character.isupper()):
                split_ls[2] = split_ls[2].replace(character, ' '   character.lower())
        split_ls[2] = split_ls[2].strip()
        return split_ls[2]
    else: #combining
        split_ls[2] = split_ls[2].split(' ')
        for i in range(len(split_ls[2])):
            split_ls[2][i] = split_ls[2][i].title()
        if (split_ls[1] == 'C'): #class
            return ''.join(split_ls[2])
        else: #method,variable
            split_ls[2][0] = split_ls[2][0].lower()
            if (split_ls[1] == 'V'): #variable
                return ''.join(split_ls[2])
            else: #method
                return ''.join(split_ls[2])   '()'

Thanks everyone for having a look at my code :)

CodePudding user response:

There is a difference in returning the string or printing it.

  • When printing it the \n character does actually get shown as a newline
  • With the return statement from a function it continues to be shown as \n in the string until you print(string)

CodePudding user response:

Your method returns the string. You have to print the string to view the new lines. Here:

print(feeder(test))

Outputs:

plastic cup 
mobilePhone 
CoffeeMachine 

Print will evaluate the new line character and reflect it as such.

Hope this helped :) Cheers!

CodePudding user response:

The difference here is looking at the result as a raw string vs printed.

When looking at the raw string, the \n new lines are escaped as \n

'plastic cup \nmobilePhone \nCoffeeMachine'

While if you print it, it should interpret \n as to print to a new line, giving you something like this:

plastic cup 
mobilePhone 
CoffeeMachine
  • Related