Python – Format – Strings

String Format

As we learned in the Python Variables chapter, we cannot combine strings and numbers like this:

Example

age = 36
txt = "My name is John, I am " + age
print(txt)

But we can combine strings and numbers by using f-strings or the format() method!


Continue reading Python – Format – Strings

Python – String Concatenation

String Concatenation

To concatenate, or combine, two strings you can use the + operator.

Example

Merge variable a with variable b into variable c:

a = "Hello"
b = "World"
c = a + b
print(c)

Continue reading Python – String Concatenation

Python – Modify Strings

Python has a set of built-in methods that you can use on strings.

Upper Case

Example

The upper() method returns the string in upper case:

a = "Hello, World!"
print(a.upper())

Lower Case

Example

The lower() method returns the string in lower case:

a = "Hello, World!"
print(a.lower())

Continue reading Python – Modify Strings

Python – Slicing Strings

Slicing

You can return a range of characters by using the slice syntax.

Specify the start index and the end index, separated by a colon, to return a part of the string.

Example

Get the characters from position 2 to position 5 (not included):

b = "Hello, World!"
print(b[2:5])

Note: The first character has index 0.

Continue reading Python – Slicing Strings

Python Strings

Strings

Strings in python are surrounded by either single quotation marks, or double quotation marks.

‘hello’ is the same as “hello”.

You can display a string literal with the print() function:

Example

print("Hello")
print('Hello')

Quotes Inside Quotes

You can use quotes inside a string, as long as they don’t match the quotes surrounding the string: Continue reading Python Strings

Python Casting

Specify a Variable Type

There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.

Casting in python is therefore done using constructor functions:

  • int() – constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number)
  • float() – constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)
  • str() – constructs a string from a wide variety of data types, including strings, integer literals and float literals

Continue reading Python Casting

Python Numbers

Python Numbers

There are three numeric types in Python:

  • int
  • float
  • complex

Variables of numeric types are created when you assign a value to them:

Example

x = 1    # int
y = 2.8  # float
z = 1j   # complex

To verify the type of any object in Python, use the type() function : Continue reading Python Numbers

Python Data Types

Built-in Data Types

In programming, data type is an important concept.

Variables can store data of different types, and different types can do different things.

Python has the following data types built-in by default, in these categories:

Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType

Getting the Data Type

You can get the data type of any object by using the type() function:

Example

Print the data type of the variable x:

x = 5
print(type(x))

Continue reading Python Data Types

Python – Global Variables

Global Variables

Variables that are created outside of a function (as in all of the examples in the previous pages) are known as global variables.

Global variables can be used by everyone, both inside of functions and outside.

Example

Create a variable outside of a function, and use it inside the function

x = "awesome"

def myfunc():
  print("Python is " + x)

myfunc()

If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value. Continue reading Python – Global Variables

Python – Output Variables

Output Variables

The Python print() function is often used to output variables.

Example

x = "Python is awesome"
print(x)

In the print() function, you output multiple variables, separated by a comma:

Example

x = "Python"
y = "is"
z = "awesome"
print(x, y, z)

You can also use the + operator to output multiple variables: Continue reading Python – Output Variables