Professional Documents
Culture Documents
Python Basics & Strings Cheat Sheet: by Via
Python Basics & Strings Cheat Sheet: by Via
print("Hello World!") Casting is used to specify a type on to a variable and this is done
using constructor functions.
Python Comments Examples
x = int(5) #x = 5
Example 1
x = int(2.8) #x = 2
#This is a comment
x = float(5) #x = 5.0
Example 2
x = float(2.8) #x = 2.8
#This is a comment
#written in
Python Strings
#more than just one line
Operations on strings and examples:
Example 3
""" This is a comment Multiline strings
Negative Indexing
Python Data Types
x = "Python Programming"
Text Type: str
print(x[-15:-13])
Numeric Types: int, float, complex
>>> ho
Sequence Types: list, tuple, range
String Length
Mapping Type: dict
x = "Hello"
Set Types: set, frozenset print(len(x))
Boolean Type: bool >>> 5
Binary Types: bytes, bytearray, memoryview Remove any whitespace from the beginning or the end
Python Strings
Example Data Type Return the string in upper case
x = "Color" list x = Hello
x = 2j complex x = "Hello"
print(x.replace("He","A")) #return "Allo"
x = ["Blue","Red","Yellow"] list
Choose a separator and split string into substrings
x = ("Blue","Red","Yellow") tuple
x = "Python Programming"
x = range(8) range
print(x.split(" ")) # return ['Python' , 'Progr‐
x={"Age":25,"Height":1.72} dict amming']
x = {"Pink","Red"} set Check if a string is present in a text
x = frozenset({"Pink","Red"}) frozenset txt = "Tunisia is a North African country"
x = True bool x = "North" in txt