Category Archives: Library

8.3 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 + bprint(c) Output:HelloWorld Example To add a space between them, add a ” “:a = “Hello”b = “World”c = a + ” ” + bprint(c) Output:Hello World

8.2 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()) Output:HELLO, WORLD! Lower Case Example The lower() method returns the string in lower case:a = “Hello, World!”print(a.lower()) Output:hello, world! Remove Whitespace Whitespace is the space before and/or after the actual text, and very… Read More »

8.1 Slicing Strings

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]) Output:llo Note: The first character has index 0. Slice From the… Read More »

8. 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’) Output:HelloHello Assign String to a Variable Assigning a string to a variable is done with the variable name followed by an equal sign and the string: Example… Read More »

7. 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… Read More »

6. 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    # inty = 2.8  # floatz = 1j   # complex To verify the type of any object in Python, use the type() function: Example print(type(x))print(type(y))print(type(z)) Output:<class ‘int’><class ‘float’><class ‘complex’> Int Int, or integer, is a… Read More »

5. 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… Read More »

4.4 Global Variables

Variables that are created outside of a function (as in all of the examples above) 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() Output:Python is awesome… Read More »

4.3 Output Variables

The Python print statement is often used to output variables. To combine both text and a variable, Python uses the + character: Example x = “awesome”print(“Python is ” + x) Output:Python is awesome You can also use the + character to add a variable to another variable: Example x = “Python is “y = “awesome”z =  x + yprint(z) Output:Python is awesome For numbers, the + character… Read More »

4.2 Assign Multiple Values

Many Values to Multiple Variables Python allows you to assign values to multiple variables in one line: Example x, y, z = “Orange”, “Banana”, “Cherry”print(x)print(y)print(z) Output: OrangeBananaCherry Unpack a Collection If you have a collection of values in a list, tuple etc. Python allows you extract the values into variables. This is called unpacking. Example Unpack a list: fruits… Read More »