Python String Cheat Sheet
Field Width and Alignment
| |
| Output: |
| Output: |
Python Strings Cheat Sheet by NouhaThabet via cheatography.com/103894/cs/21332/ Python Strings Operations on strings and examples: Mult iline strings x = ' ' ' This is a multiline string ' ' ' Get the character at a specific position x = ' Python.
- Whether you’re working with string methods or built-in functions in Python, this Cheat Sheet helps you program the correct order for the operation so you achieve the correct result.
- Python Cheat Sheet STRINGS A sequence of unicode (Python3) or ASCII (Python2) characters. MyStr = 'Hello World' Strings are specified with double or single quotes.
Member and Element Access
| Output: |
| Implicit positional arguments (2.7 and above only) |
| Access keyword arguments by name |
| Access element by index |
| Access element attribute |
| Access element by key |
Numerical Representation
| Output: |
| Output: |
| Output: |
| Output: |
| Output: |
| Output: |
| Output: |
| Output: |
| Like |
| Exponent notation |
| Exponent notation (capital 'E') |
Cisco webex teams for mac. | Fixed point |
| General format |
| Percentage |
| Precision |
Conversions
| Output: |
| Output: |
See also Python Programming - Getting Started
Python String Manipulation Cheat Sheet
Following are terse descriptions for Python3 keywords, programming concepts, and commonly used functions. For more information, see w3schools for a bit more explanation and python.org for the language reference.
Python F String Cheat Sheet
- 1Keywords
The following are heavily used in all levels of Python programming. This is a minimum set of keywords to know how to use.
break
- exit the current loopcontinue
- in a loop, go to next iteration of the loopis
- determine whether two objects are the same object (not just value)import, from
- to load a moduledef
- declare a functionwhile
- loop with only conditionif, elif, else
- conditional statements (three keywords)for
- loop with that iterates through a listreturn
- keyword to send a value back from a functionand
- boolean operator, True only if both sides are Trueor
- boolean operator, True if either side is Truenot
- boolean operator, negatesTrue, False
- boolean values (two keywords)del
- remove from a list by positiontry, except
- handle an exception, basic use (2 keywords)raise, assert
- raise/indicate an exception (2 keywords)in
- test if something is inside of a list/string/tuple/dictionaryNone
- special value for a variable that has no valuepass
- empty statement that does not do anything
More Keywords
These keywords are often not introduced or heavily used until the second Python course.
Python String Library Cheat Sheet
as, finally, with, else
- exception handling, more keywords (4 keywords)class
- defining new class data type (for object-oriented programming)lambda
- create unnamed / anonymous functionglobal, nonlocal
- access variables outside of current scope (2 keywords)async, await
- for writing asynchronous code with asynchio package (2 keywords)yield
- for creating generator functions
See this video for a demo and explanation of basic data types and variables.
These are terms that we use to describe programs. These are terms that have a precise meaning when talking about programs.
- string - text data type
- boolean - data type for True and False
- floating point - data type that stores numbers with fractional parts (e.g., 3.14 or 2.2)
- integer - data type that stores only integers
None
- a special value in Python that means 'nothing' but is different than 0, False, and '- variable - name for a place in memory to store values. Two basic things you do with a variable - (i) store a value into the variable, (ii) get the value from the variable.
- keywords - also called reserved words - these are names that should not be used for variable names because they have special meaning to python. Example:
for
is used for loops and shouldn't be used as a variable name. - function - also called procedures or methods - name for a block of code that does something and that your code can use when needed. Two basic things you can do with a function - (i) define what the function is (specify the code for the function), (ii) call the function later on in your code. Three main parts of defining function - (a) function code (called the body), (b) function parameters (aka input variables), (c) function return values. Three main parts of calling/using a function - (1) specify the arguments to the function (which are passed in to the parameters of the function), (2) call the function, (3) get the return value of the function. Note that the function prototype refers to the line that declares the function, which has the function name, return type, and list of parameters to the function. When a function is called, the arguments to the function are passed to the function (parameter variables are set equal to the arguments).
- flow chart - diagram that shows the steps / flow of control in a program (also used to diagram decision-making in other settings - e.g., diagnosis of an illness, managing a factory, ..)
- binary operator - operator that takes two values to produce a result. An example is addition, 2 + 3 results in 5. Another example is comparison, 'hi' 'bye' results in False because the two are not equal.
- unary operator - operator that takes one value to produce a result. An example is Boolean not, not True results in False.
- operator precedence - rules for which operators are evaluated first in an expression. For example, in 1 + 2 * 3, the multiplication is performed first, giving 1 + 6, and then the + is performed to result in 7. For Python, see python.org.
- operator associativity - rule whether operators of the same type are evaluated left-to-right or right-to-left. Math operators are left-to-right. For Python, see python.org.
Python String Function Cheat Sheet
print
- function to write to the screenint, float, str
- functions to convert to integer, floating point number, or stringrange
- function to generate a sequence of numberslen
- function to get the length of a string, list, or tupleopen, read, write, close
- functions for reading/writing text files and options 'r', 'w', 'a' for open- string methods - isalpha, isdigit, split, join
- list methods -
sort, index, append, remove, copy
- tuple methods -
index, count
- dictionary methods -
keys, values, items
- syntax for using strings, lists, tuples dictionaries -
- methods in random module -
randint, shuffle
- methods in sys module -
exit, argv
list - methods from the copy module -
copy.deepcopy