String transformation in python

Asked

Viewed 110 times

-1

'Cause this way there’s no mistake:

print(f'Seu nome possui {len(nome.replace(" ", ""))} letras')

But this one there:

print(f'Seu nome possui {len(nome.replace(' ', ''))} letras')

Python does not treat single quotes and double quotes as synonyms?

1 answer

0


Problem

Here he understands that all content is one string complete, since you start with single quotes and end with single quotes, and he understands double quotes as part of the string, since you start the instruction with single quotes.

print(f'Seu nome possui {len(nome.replace(" ", ""))} letras')

Here the situation is different, he interprets the instruction as if it were passed 3 strings different separated with space, since you started with simple quotes, it waits for the end with simple quotes.

print(f'Seu nome possui {len(nome.replace(' ', ''))} letras')

How the strings got separated

String 1: 'Its name has {Len(name.replace('

String 2: ', '

String 3: '))} letters'

An example

I’ll give a simpler example to see the problem

It’s like you want to do the print as follows:

print('oi' 'eu' 'sou' 'assim')

And the right thing would be

print('oi eu sou assim')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.