Python Strings
Python has the string data type to store the string values. While we are creating a string varible, a Python string object is created for storing the string values. A string is a sequences of chracters. Anything within the quotes is considered as a string. we can use single or double quotes.
We can create a single line within quotes (' single line text ') and within double quotes (" single line text "). If we want to create a multitline text, we can use three double quotes (""" mulitiline text """).
The Python tutorial on Techtuts provides an information about Python and Python 3 from the basis to high level. This tutorial provides a good understanding about the Python and Python for Technical, Non-Technical People, College Students and Working Professionals. Given examples are an easy to write and simple to practice by yourself.
In this tutorial you will learn about the Python and Python3. This tutorial includes an Introduction, Python Environment Setup, Variables, Data types , Control flow Statements, Loops, Functions, Data Structures, Modules ,Files, Classes, Python Web Frameworks etc.
String datatype, variable and values
The following program is example for string datatypes, variables and values.
>>> str1 = "This is a string."
>>> print(type(str1))
<class 'str'>
>>> print(str1)
This is the string.
>>> str2 = 'This is also string.'
>>> print(type(str2))
<class 'str'>
>>> print(str2)
This is also string.
Use correct syntax with strings
While your are creating a string varaible, use the correct syntax. For example, a string variable value includes an apostrophe between double quotes, The python interpreter easily identify. But a string variable value includes an apostrophe within single quotes, It cannot identify and gives the error.
>>> str1 ="This is techtut's python strings tutorial"
>>> print(type(str1))
<class 'str'>
>>> print(str1)
This is techtut's python strings tutorial
>>> str2 = 'This is techtut's python strings tutorial'
File "<stdin>", line 1
str2 = 'This is techtut's python strings tutorial'
^
SyntaxError: invalid syntax
>>>
To avoid this error simply use \' with in the string while creating a string varibale with single quotes. Whenever you are creating a string variable you have to be a careful. You follows this, wherever you needed.
>>> str2 = 'This is techtut\'s python strings tutorial'
>>> print(str2)
This is techtut's python strings tutorial
>>>