python logo

Type Casting

Type casting is the process of converting one data type to another data type, like a string to an integer or float and vice versa. Example: num_string1 = "30" num_string2 = "20" num_strings = num_string1 + num_string2 print(num_strings) print(type(num_strings)) nums_typecast = int(num_string1) + int(num_string2) print(nums_typecast) print(type(nums_typecast)) try_typecast = int(num_string1 + num_string2) print(try_typecast) print(type(try_typecast)) In the code above, the data type of num_string1 and num_string2 are strings so when they are added using + operator, the result will just be a string concatenation and the data type is still a string....

February 12, 2022 · 1 min · Kei
python logo

Special Characters

Special characters in strings are: \ (Backslash) : an escape character \n : specifies new line within the string \t : adds a tab print("My name is Marshmallow. \ I wag my tail when I'm happy. \ I bark at speedy motorcycles \ and unwanted visitors.") print("\n") print("\\n") print("My name is Marshmallow.\nI wag my tail when I'm happy.\nI bark at speedy motorcycles\nand unwanted visitors....

February 11, 2022 · 1 min · Kei
python logo

Print Formatting

There are several ways to output the result using print() apples_in_basket = "111" print("There are " + apples_in_basket + " apples in the basket") print("There are",apples_in_basket,"apples in the basket") print("There are {}apples in the basket".format(apples_in_basket)) print(f"There are {apples_in_basket}apples in the basket")

February 11, 2022 · 1 min · Kei
python logo

String Methods and Functions

Methods and Functions to use on Strings The Python Standard Library is the standard library that is distributed with Python. This library reference manual is available at Python Standard Library Some of the functions and methods I can use on string objects are: len(), type(), id(), capitalize(), upper(), lower(), strip(), find(), split(), join() Functions do not tied to the object. I have to type the function name followed by parentheses and pass in the object as an argument....

February 11, 2022 · 2 min · Kei
python logo

Strings Concatenation, Indexing, Slicing

Concatenation Strings can be added to each other name = "Kei" message = "Good morning," print(message + name) Strings are immutable, it can’t be changed although it can be reassigned with the same variable name but they are stored in different memory location name = "Kei" message = "Good morning," print(id(message)) message = message + " " + name print(id(message)) Indexing Strings are sequence of characters therefore they can be indexed....

February 11, 2022 · 1 min · Kei
python logo

Strings

Strings in Python can be represented by wrapping them by using ’ ’ (single quotes), " “(double quotes), or ’’’ ’’’ (triple quotes) 'This is Hello World using single quotes.' "This is Hello World using double quotes." """This is Hello World using triple quotes This is also called multi-line strings.""" To display the string as an output to the screen I can use Python bulit-in function, print() print('This is Hello World using single quotes....

February 11, 2022 · 1 min · Kei