Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use quotes in output

My code works like it is supposed to.
I want the string to be in quotes, for example

  • "Kayak" is a palindrome
  • "Dog" is not a palindrome

But when I include quotes in the code then it just says
a is a palindrome

    #!/usr/bin/python3 
    a = input("Enter a word or sentence: ")
    
    b = a.lower()
    
    first = 0
    last = len(b) - 1
    pal = True
    
    while first < last:
        if b[first] == ' ':
            first += 1
            continue
        if b[last] == ' ':
            last -= 1
            continue
        if b[first] == b[last]:
            first += 1
            last -= 1
        else:
            pal = False
            break
    
    if pal:
        print(a, "is a palindrome")
    else:
        print(a, "is not a palindrome")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you put anything in quotes, python assumes it is a string. That means

print("a")

prints a. If you want "a" to be printed you need to tell python to print the quotes and you text. Such as

print("\"a\"")

In your case you can write:

print("\"" + a + "\"", "is a palindrome")  

For more information you can look into https://docs.python.org/3/tutorial/inputoutput.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading