python if string equals
Python hosting: Host, run, and code Python in the cloud!
A string in Python represents a sequence of characters and is a fundamental data type in the language. Strings are predominantly utilized for displaying and manipulating text.
Strings can be defined by enclosing text within quotes. Python supports various types of quotes, including single ('
), double ("
), and triple ('''
or """
).
Related Course:
Python Programming Bootcamp: Go from zero to hero
Displaying and Receiving String Input
To display a string on the screen, you can use the
print
function. For instance:
s = "hello world" |
If you need to retrieve a string input from the user via the keyboard, the input
function comes in handy:
name = input("Enter name: ") |
Note: If you are utilizing an older version of Python (2.x), the raw_input
function is used instead of input
:
name = raw_input("Enter name: ") |
To ascertain your Python version, you can execute the command:
python –version
Comparing Strings in Python
In Python, the equality operator (
==
) lets you check if two strings are identical. For example:
sentence = "The cat is brown" |
Conversely, to see if two strings are different, employ the inequality operator (!=
):
sentence = "The cat is brown" |
Enhance your Python skills further with exercises:
Download Python Exercises
For more tutorials, navigate:
Leave a Reply:
print s[6:] # prints everything from the 5th character
Shouldn't this say 'prints everything from the 7th character (including the 7th)
I meant the characters after the 5th character, thanks for noticing, updated it.
im still new to the language and im eager to learn more about it,i was doing c++ for the past two years and i see the python as a new challenge that im willing to take
Hi tumelo, we hope you enjoy our site. More tutorials coming soon.
its simply superb i really enjoying it
in my python intepreter , i need parenthese after the 'print' function
Hi, you are likely using Python 3. Its only a small difference, in Python 2.5, 2.7 parenthese is not needed.
i use python 3.
However it replied:
SyntaxError: invalid syntax
Hi, change your code to:
Python is strict on the indention (spaces) and print cannot be on the same line as else.
Hi, I use python 2.7. I put instructions else: and print in the same line. And it works !
Hi Frank. what you are doing here is very much appreciated. thank you. what interpreter do you recommend to use on os x? i am using the idle but can't put codes in multiple lines. it just keeps executing the first line.
Hi Huso, I don't have os x at home right now, but these steps should work:
If you want to use idle go through these steps:
- press file -> new file
- in the new window that appears, type your program code
- press F5
- save the program as example.py
- you now see output in the other window
To restart, press F5 again.As in the screenshot:
I ran idle from linux, but should be about the same.
Alternative: open a terminal, go to your program directory and type:
python program_name.py
Another IDE can also work, PyCharm is pretty good and free.
Let me know how it works out :-)
Hi Frank. Thank you for tutorials. I want to ask you a small question.
In line print s[0:2], why do you get 'He' not 'Hel'?
Because I thought, if you print s[0], s[1], and s[2], you should get 'Hel'..
Hi Stephanie, the second number is the maximum (stop) which is not included. s[0:1] prints 'H'. s[0:3] prints 'Hel'.
A small trick to check the amount of characters that fit you can use the range() function:
In my python 2.7. I need to press backspace to the first character of the next line then input else:, it works!
i use python 3.4
it still an error: SyntaxError: invalid syntax
can u help me ?
Hi, should be == not ===.
Change to:
Hello. I enter this code in my python but it do not produce anything .
What is wrong?
What should be the result?
I dont understand the concept of this page can anyone help?
Hi, this should output 'not equal'. Are you using Python 3? In that case use brackets: print('equal').
Hi, these are Python computer programming tutorials. Have a look on python.org
No. I use pythton 2.7.
when I use s.replace I get a syntax error unexpected token..Can you let me know what I am missing here?
That's odd. The code works on my machine. Does other Python code run? Do you get any output?
Hi, probably a typo. Could you post your code?
print [0:2] is not printing Hel? I guess it has to output 0,1, and 2 nd characters together as a string.. Is not so?
Hi Vidhya, print(s[0:2]) returns only the 0th and 1st character.
Hi Frank ,
Can you explain me How`s this command print(s[0:2]) , catch the character.
As you above mentioned 0 means 0th character which is H but 2 denote 1st character ???
Hi Shivi, 2 is the upper limit index that is not included. In simple terms, it's your final index +1. Try this code:
How do I change the font of code?
Do you mean the font on this site or of your terminal? Python uses the standard terminal font, for a different font use a gui.
Is this codes for python 3.4.0 ? I couldn't work :(
Hi, it's updated, the codes work in both Python 3.4.0 and Python 2.7 now.
you might be missing the second bracket in the back, or what i wanted to try is using " which doesn't work as well.. it has to be '
Hi Frank!
Which Editor in used in your Videos? what is the difference between "Compile" and "Run" a code?
thanks
Saqib
what does this comment lines implies that you add in every example: "#!/usr/bin/env python"??
Thanks
Hi Saqib Ali khan,
I'm using the Atom editor https://atom.io/ with the script plugin. Then I use the shortcut ctrl+shift+b to execute prorams. The theme I use is Unity for UI theme with Atom Dark for the syntax theme. You can use ctrl+shift+p to search the application options.
Screenshots of configuration:
Atom IDE 1
Atom IDE 2
A compiled code is executed directly on the computer. If you run a code (with an interpreter), a program reads this code and does the machine instructions (in this case Python). You could say, with an interpreter you have a translator that reads the code and translates/executes it into machine language.
This is the location of python on my computer (non windows). On a Windows computer you may do: #!C:\Python33\python.exe
sir, can u pls explain start index and past index clearly with exaples
Given the string s = "hello", the start index is always [0]. The last index is the length of the string minus one.
sir? can you explain the equality operations? i don't get it......
Hi,
The equality operator (==) tests if two variables have an equal value. Given variable x=3 and y=3, we can test for equality using the statement if x == y, which will return true. If we change either x or y, it would return false. It looks like the assignment operator (=) , but the purpose of the assignment operator is only to set the variables data. You can use the operator on strings, integers, decimals and many other types of variables. To test for inequality use the not operator (!=).
oh i get it now thanks alot sir
You have spelled "concatenation" wrongly: the word is "concatenate" -- note the 'e' in the middle -- NOT "concatinate". (It means 'link together like a chain' because it comes from the Latin 'catena' = chain.)
Thanks Jem, I updated it.
s="Hello"
>>> print(s[5:]) does not print anything.
>>> print(s[6:]) does not print anything.
But if you use it like below , Output gets printed like :
>>> print(s[:5])
Hello
>>> print(s[:6])
Hello
am much interested to learn programming,,am going through tutorials but is possible to get practical tutorials
Why do I get syntax error, when I use the( ==) operator to compare equalities. For example if x = 3 and y = 3, when I press enter, I get a syntax error. Please tell me where I am going wrong.
Are you running the code or using the interpreter directly?
Do you see >>> before the text you type? If so, you have to create a file with the extension .py and execute with that as parameter.
you are trying to slice the 5th and 6th index but here max length is 5 so it will print nothing
>>> s = "hello"
>>> len(s)
5
What plugin are you using for comments
It's a custom plugin I coded during a weekend.