python 3 tutorial for beginners Tutorial

Python 3 Strings

String in Computer Programming Language is a way to store a finite sequence of characters. In Python 3 we can store a string in the string variable. A numeric variable which we discussed in our previous article only stores numeric characters 0 to 10 or dot. But a string variable can store letters, numerals, symbols and punctuation marks. Creating a string variable is similar as creating a numeric variable. We need to first provide the variable_name = then our string in single, double quotes, or three times double quotes. There is no difference which one you used for your string.

Unicode Strings: In Python 3, all strings are Unicode text. Python 2 Strings are ASCII Text. You can check this article for the difference between Unicode and ASCII. In brief, ASCII defines 128 characters, which mapped from 0 to 128. And Unicode defines (less than) 2 to the power 21 (2^21 = 2097152) characters mapped from 0 to 2^21. Definitely, it is one of the reasons to switch to Python 3, if you are still using Python 2.

Following are some example for string variables.

author_name='Mayank Sanghvi'
tripple_quotes = """Here we used tripple quotes for a string. It's work"""
are_you_kidding='''It is also going to work'''

Triple Quotes

In case of triple quotes, we can split our strings in multiple lines.

python_tutorial="""Hello everyone, This is Mayank Sanghvi
from and I am back with another "Python Tutorial Video". In this video, we are going to learn about strings with triple quotes. In which we can include Tabs  (\t ) New Line [ \n ].
It is good for writing blog post contents because we can use single quotes (') and double quotes (") also."""

You can also single and double quotes simultaneously. But you need to ensure the start and end quotes must match with each other

python_tutorial_price="It's Free"
# did_you_forgot_to_read_above_point = 'Hello, Mayank"
# machine_learning_tutorial_price = 'It's Free'

Escape Characters

The backslash(\) character is used to escape characters that have special meanings.

python_2_tutorial = 'It\'s Not Available'

What is an Escape Characters?

In the programming language, escape characters or nonprintable characters used to escape the special meanings for the following special character. In the above esample we are escaping the special maning for single quote (‘) by using backslash (\).

The following table is the list for escape characters supported in Python 3.

Escape Character Purpose
\newline Ignore
\\ Backslash(\)
\’ Single Quote (‘)
\” Double Quote
\a ASCII Bell (BEL)
\b ASCII Baskspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\v ASCII Vertical Tab (VT)
\ooo Character with octal value ooo
\xhh Character with hex value hh

Python 3 String Special Operators

In Python 3 we can use following string special operators to perform some quick operations. 
For the testing, we create two variables first_name="Mayank " and last_name="Sanghvi".

Operator Description Example Output
+ Concatenation: Add two strings. first_name + last_name ‘Mayank Sanghvi’
* Repetition: You can repeat string inside a variable using the star(*) operator. first_name*2 ‘Mayank Mayank ‘
not in      


Leave A Comment

Please login to post your valuable comments.