To define a string simply type text between quotes. Python accepts single, double and triple quotes.
- Automate the Boring Stuff with Python Programming
- Complete Python Bootcamp: Go from zero to hero in Python
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:
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')