Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Python Basics & Strings Cheat Sheet

by Nouha_Thabet via cheatography.com/103894/cs/21291/

Hello World in Python Python Casting

print​("Hello World!​") Casting is used to specify a type on to a variable and this is done
using constr​uctor functions.
Python Comments Exam​ples
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 Mult​iline strings

written in x = "​"​" This is a


more than just one line "​"​" multiline string​"​"​"

Get the character at a specific position


Python Variables
x = "​Python Progra​mmi​ng"
x = "How old are you ?" #x is of type str print(​x[1]) ​#print character at position 1
print(x) >>> y
>>> How old are you ? Slic​ing
x = 25 #x now is of type int
x = "​Python Progra​mmi​ng"
print(x)
print(​x[3:5])
>>> 25
>>> ho

Negative Indexing
Python Data Types
x = "​Python Progra​mmi​ng"
Text Type: str
print(​x[-​15:​-13])
Numeric Types: int, float, complex
>>> ho
Sequence Types: list, tuple, range
String Length
Mapping Type: dict
x = "​Hel​lo"
Set Types: set, frozenset print(​len(x))
Boolean Type: bool >>> 5

Binary Types: bytes, bytearray, memory​view Remove any whitespace from the beginning or the end

Get the data type of a variable "​var​" x = "​ Hello "


print(​x.s​trip()) #return "​Hel​lo"
type(​var)
Return the string in lower case
Python Data Types Examples x = Hello
print(​x.l​ower()) #return "​hel​lo"

Python Strings
Exam​ple Data Type Return the string in upper case
x = "​​Co​lor​" list x = Hello

x = 1 int print(​x.u​pper()) #return "​HEL​LO"

x = 1.2 float Replace a string with another string

x = 2j complex x = "​Hel​lo"
print(​x.r​epl​ace​("He​"​,"A")) ​ #return "​All​o"
x = ["Bl​​ue​"​,​"​Red​​"​,​"​Ye​​llo​​w"] list
Choose a separator and split string into substr​ings
x = ("Bl​ue",​"​Red​"​,"Ye​llo​w") tuple
x = "​Python Progra​mmi​ng"
x = range(8) range
print(​x.s​pli​t(" ")) # return ['Python' , 'Progr​‐
x={"Ag​e":2​5,"H​eig​ht":​1.72} dict amm​ing']
x = {"Pi​nk",​"​Red​"} set Check if a string is present in a text
x = frozen​set​({"P​ink​"​,"Re​d"}) froze​nset txt = "​Tunisia is a North African countr​y"
x = True bool x = "​Nor​th" in txt

x = b"Co​lor​" bytes print(x) # return True

x = bytear​ray(8) bytea​rray Conc​ate​nat​ion

x = memory​vie​w(b​yte​s(8)) memor​yview x = "​Hel​lo"


y = "​Wor​ld"
Get the data type of x :
z = x + " " + y
x = "​Col​or"
print(z) # return "​Hello World"
print(​typ​e(x))
Insert numbers into strings
>>> str
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {}
dollar​s."
print(myorder.format(quantity, itemno, price))

By Nouha_Thabet Published 4th December, 2019. Sponsored by Readable.com


cheatography.com/nouha- Last updated 6th December, 2019. Measure your website readability!
thabet/ Page 1 of 2. https://readable.com

You might also like