Reading about Python? Actually practice it. Try PyChallenge free

Python Tutorial

Python strings

Python hosting: PythonAnywhere — host, run and code Python in the cloud. Free tier available.

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:
Practice Python with interactive exercises

String input and output

To output text (string) to the screen:

s = "hello world"
print(s)

To get text from keyboard:

name = input("Enter name: ")
print(name)

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

name = raw_input("Enter name: ")
print(name)

To test your version: python --version

String Comparison

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

#!/usr/bin/python

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

#!/usr/bin/python

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

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

BackNext

Practice
Stop reading. Start writing Python.
PyChallenge gives you interactive exercises in your browser — no install needed.
Practice Python with interactive exercises