0

I have a string value printing in multiple lines. I used three single/double quotes for assigning string value

artist = """
       Mariah
       Carey
       """
 name = 'My all'
 realeased_year = 1997
 genre = 'Latin'
 duration_seconds = 231

print(f'{artist}(full name has {len(artist.strip().replace(" ", ""))} characters) and her song \'{name}\' was released in {realeased_year + 1}')

enter image description here It outputs wrong number of characters: 12

However, when I assign a string value in one line like

artist = 'Mariah Carey'

I have correct number of characters 11 enter image description here

Is it possible to remove all white spaces (leading, middle and trailing) from multi line string value without using regular expressions

1
  • If you try to artist = "Marian Carey" and print(len(artist.strip())). You will find the same 12 Character length output. Please Check again. Commented Jan 5, 2021 at 3:10

2 Answers 2

3

str.split() splits a string on whitespace so you could do this:

>>> artist = """
...        Mariah
...        Carey
...        """
>>> ' '.join(artist.split())
'Mariah Carey'

This will split the string into a list of words. Those words are then joined with a single space delimiter.

I assumed that you would want to retain one interword space, however, if you want to get rid of all whitespace then join with an empty string:

>>> ''.join(artist.split())
'MariahCarey'

Modify your display string:

>>> print(f'{artist}(full name has {len("".join(artist.split()))} characters)')

   Mariah
   Carey
   (full name has 11 characters)
Sign up to request clarification or add additional context in comments.

7 Comments

do you think you can provide a complete version for your answer ? running into syntax error Thanks
@DurdonaA.: looks like a complete version to me... I've cut and paste verbatim from terminal.
formatting with {f""} getting a syntax error
Use double quotes like this: "".join()
@DurdonaA.: you're welcome. Please consider accepting this answer if you think it answers your question.
|
1

When you use triple quotes and use enter to separate your lines, python inserts \n character which is also added to the total number of characters. So in the following line

print(f'{artist}(full name has {len(artist.strip().replace(" ", ""))} characters) and her song \'{name}\' was released in {realeased_year + 1}')

in the replace function instead of a " " use "\n".

3 Comments

getting an error SyntaxError: f-string expression part cannot include a backslash
If you have to or want to use f-string then you can assign len(artist.strip().replace(" ", "")) to a variable and use that variable in your print statement. eg. length = len(artist.strip().replace(" ", "")) and use length in print.
Its work perfectly as per the question requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.