python logo

Python strings

Python hosting: Host, run, and code Python in the cloud!
python-string a string, series of characters

A string is a series of characters, they are mostly used to display text.

To define a string simply type text between quotes. Python accepts single, double and triple quotes.

Related Course:
Python Programming Bootcamp: Go from zero to hero

String input and output

To output text (string) to the screen:

s = "hello world"

To get text from keyboard:

name = input("Enter name: ")

If you use an old Python version (2.x), you need to use:

name = raw_input("Enter name: ")

To test your version:
python –version

String Comparison

To test if two strings are equal use the equality operator (==).


sentence = "The cat is brown"
q = "cat"

if q == sentence:
print('strings equal')

To test if two strings are not equal use the inequality operator (!=)


sentence = "The cat is brown"
q = "cat"

if q != sentence:
print('strings equal')

If you are new to Python programming, I highly recommend this book.

Download Python Exercises


Leave a Reply:

Mpk Tue, 28 Apr 2015

print s[6:] # prints everything from the 5th character

Shouldn't this say 'prints everything from the 7th character (including the 7th)

Admin Tue, 28 Apr 2015

I meant the characters after the 5th character, thanks for noticing, updated it.

Tumelo Mon, 25 May 2015

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

Frank Mon, 25 May 2015

Hi tumelo, we hope you enjoy our site. More tutorials coming soon.

Guru Tue, 26 May 2015

its simply superb i really enjoying it

Sanfusu Sat, 13 Jun 2015

in my python intepreter , i need parenthese after the 'print' function

Frank Sat, 13 Jun 2015

Hi, you are likely using Python 3. Its only a small difference, in Python 2.5, 2.7 parenthese is not needed.

Student Mon, 22 Jun 2015

i use python 3.

sentence=' the cat is brown'
if q ==sentence:
else:print('not equal').

However it replied:
SyntaxError: invalid syntax

Frank Mon, 22 Jun 2015

Hi, change your code to:

#!/usr/bin/env python3

sentence='the cat is brown'

if q == sentence:
print('not equal')

Python is strict on the indention (spaces) and print cannot be on the same line as else.

Ahmed Sun, 28 Jun 2015

Hi, I use python 2.7. I put instructions else: and print in the same line. And it works !

Huso Sun, 28 Jun 2015

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.

Frank Sun, 28 Jun 2015

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
  • 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:

Another IDE can also work, PyCharm is pretty good and free.

Let me know how it works out :-)

Stephanie Mon, 29 Jun 2015

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'..

Frank Mon, 29 Jun 2015

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:

>>> range(0,3)
[0, 1, 2]
>>> range(0,2)
[0, 1]
Le Tuan Mon, 06 Jul 2015

In my python 2.7. I need to press backspace to the first character of the next line then input else:, it works!

Myph Sat, 11 Jul 2015

i use python 3.4

sentence = "i love cat"
q = "cat"
if q === sentence:
print('not equal')

it still an error: SyntaxError: invalid syntax
can u help me ?

Frank Sat, 11 Jul 2015

Hi, should be == not ===.
Change to:

sentence = "i love cat"
q = "cat"
if q == sentence:
print('not equal')

Pavlin Angelov Sun, 26 Jul 2015

Hello. I enter this code in my python but it do not produce anything .

sentence = "The cat is brown"
q = "cat"

if q == sentence:
print 'equal'
print 'not equal'

What is wrong?
What should be the result?

Abdullah Sun, 26 Jul 2015

I dont understand the concept of this page can anyone help?

Frank Sun, 26 Jul 2015

Hi, this should output 'not equal'. Are you using Python 3? In that case use brackets: print('equal').

Frank Sun, 26 Jul 2015

Hi, these are Python computer programming tutorials. Have a look on

Pavlin Angelov Mon, 27 Jul 2015

No. I use pythton 2.7.

Mady Mon, 27 Jul 2015

when I use s.replace I get a syntax error unexpected token..Can you let me know what I am missing here?

Frank Mon, 27 Jul 2015

That's odd. The code works on my machine. Does other Python code run? Do you get any output?

Frank Mon, 27 Jul 2015

Hi, probably a typo. Could you post your code?

Vidhya n Tue, 28 Jul 2015

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?

Frank Tue, 28 Jul 2015

Hi Vidhya, print(s[0:2]) returns only the 0th and 1st character.

Shivi Thu, 30 Jul 2015

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 ???

Frank Fri, 31 Jul 2015

Hi Shivi, 2 is the upper limit index that is not included. In simple terms, it's your final index +1. Try this code:

s = "Hello World"
Samuel Wed, 12 Aug 2015

How do I change the font of code?

Staff Wed, 12 Aug 2015

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.

Ilkay Sun, 16 Aug 2015

Is this codes for python 3.4.0 ? I couldn't work :(

Frank Sun, 16 Aug 2015

Hi, it's updated, the codes work in both Python 3.4.0 and Python 2.7 now.

Anton Fri, 21 Aug 2015

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 '

Saqib Ali Khan Sat, 22 Aug 2015

Hi Frank!
Which Editor in used in your Videos? what is the difference between "Compile" and "Run" a code?

Saqib Ali Khan Sat, 22 Aug 2015

what does this comment lines implies that you add in every example: "#!/usr/bin/env python"??

Frank Sat, 22 Aug 2015

Hi Saqib Ali khan,

I'm using the Atom editor 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.

Frank Sat, 22 Aug 2015

This is the location of python on my computer (non windows). On a Windows computer you may do: #!C:\Python33\python.exe

Shilpa Mon, 31 Aug 2015

sir, can u pls explain start index and past index clearly with exaples

Frank Mon, 31 Aug 2015

Given the string s = "hello", the start index is always [0]. The last index is the length of the string minus one.

Chan Wed, 02 Sep 2015

sir? can you explain the equality operations? i don't get it......

Frank Wed, 02 Sep 2015


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 (!=).

Chan Thu, 03 Sep 2015

oh i get it now thanks alot sir

Jem Fri, 11 Sep 2015

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.)

Frank Fri, 11 Sep 2015

Thanks Jem, I updated it.

Tarik Makhija Mon, 14 Sep 2015

>>> 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])
>>> print(s[:6])

Geoffrey Mon, 21 Sep 2015

am much interested to learn programming,,am going through tutorials but is possible to get practical tutorials

Ramu Thu, 10 Dec 2015

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.

Frank Thu, 10 Dec 2015

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.

saravanan Tue, 22 Mar 2016

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)

John Thu, 05 May 2016

What plugin are you using for comments

Frank Fri, 06 May 2016

It's a custom plugin I coded during a weekend.