In the part one of this series, we showed you how to use variables in Python 3. In this tutorial, we are going to work with Strings and Numbers.
Note: Before you start, make sure you run each given code to understand that how things work!

Strings

String is a data type in programming language, strings in Python are very simple at initial glance but we can use them in many ways.
A string is a sequence of letters, numbers or symbols enclosed in single or double quotation marks. String are immutable data type that means we can't change it once defined. Strings does not require to be defined in advance.

String defined using double quotation marks:
greeting = "Hello There!"

String defined using single quotes:
hint = 'Using single quotes'

Using apostrophe in a string. Well, there are two ways of doing it:
saying = "If you can't help yourself then nobody will."
The other way of using apostrophe with escape character, if you still want to use single quotes:
saying = 'If you can\'t help yourself then nobody will.'

Combining and concatenating strings using addition operator, in simple words you can join multiple strings:
example_concatenation_string = "First part" + ' and ' + "last part"
print("Lap" + 'top')
print("Another: " + example_concatenation_string)

Multiplying a string, you will find it useful later:
example_multiply = "Hey There!"*5

Know the length of the string using len function:
print(len("Hey There!"))
length_of_string = "This is a string."
print(len(length_of_string))

Strings can be written on multiple lines:
quote = """A person who never made a
 mistake never tried 
 anything new."""
print(quote)

Find out if the sub-string is present in a string:
quote = "A person who never made a mistake never tried anything new."
print("mistake" in quote)

print("learning" in "You are learning Python 3.")

Special characters in strings:
We can do formatting in strings using special characters, for instance we need line break, tabs or other formatting. These formatting can be done in a string using special character called escape.

Line break in a string:
quote = "Albert Einstien said:\nA person who never made a mistake never tried anything new."
print(quote)

Tab in a string:
print("Languages:\n\tC++\n\tPython\n\tJava\n\tC")

Printing a backslash character:
print("Languages:\\C++\\Python\\Java\\C")

Making string omit the recognition of escape character:
print(r"Omitting these escape characters \t and \n.")

String formatting
Convert numerical values to string using str function:
print(str(1))
print(str(33.33))
print(str(987+11j))

Use numbers in a string:
print("He is " + str(23) + " years old.")

Strings case formatting:
The string case formatting is very useful, for instance you ask user to enter the username and it has to be unique for each user. In such case, you can collect the input from user and store it in lowercase then compare it with all the usernames in your list and if it is already taken then you can notify the user, if not you allow user to create the username.
The first one is title case formatting, it is useful for scenarios like: person or place name and so on.
name = 'albert einstein'
print(name.title())

To convert string to lower case:
name = 'ALBERT EINSTEIN'
print(name.lower())

Use this method to change case to upper:
name = 'albert einstein'
print(name.upper())
We will see string formatting in more detail in upcoming articles.

Numbers

Numbers are important data type and used in every program, for example: score in games, represent data, store information in web applications and so on. Numbers without decimal considered as integers in Python.
Python also supports the order of operations, let us use multiple operations in one expression.

Use of add (+), subtract (-), multiply (*) and divide (/) sign in the Python:
print(10+2)
print(12-2)
print(6*2)
print(24/2)

Exponents use two multiplication operators in Python:
print(2**2)
print(3**9)

Order of operation in a single expression:
print(5-1 * 2)
print((5-1) * 2)

Floats, you can use floats without worrying about how they will behave:
print(0.1 + 0.8)
print(1.1 * 2.99)

Example of a square root in Python:
print(100**0.5)

That's all folks for this lesson! Hopefully these things will help you.
Share To: