Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Sandeep
Sandeep

Posted on

     

Python Cheat Sheet Part - 2

Strings
Python string is a sequence of characters, and each character can be individually accessed using its index.

String
You can create Strings by enclosing text in both forms of quotes - single quotes or double quotes.

variable_name = "String Data"
Enter fullscreen modeExit fullscreen mode

Slicing
Slicing refers to obtaining a sub-string from the given string. The following code will include index 1, 2, 3, and 4 for the variable named var_name

var_name[1 : 5]
Enter fullscreen modeExit fullscreen mode

isalnum() method
Returns True if all the characters in the string are alphanumeric, else False

string_variable.isalnum()
Enter fullscreen modeExit fullscreen mode

isalpha() method
Returns True if all the characters in the string are alphabets

string_variable.isalpha()
Enter fullscreen modeExit fullscreen mode

isdecimal() method
Returns True if all the characters in the string are decimals

string_variable.isdecimal()
Enter fullscreen modeExit fullscreen mode

isdigit() method
Returns True if all the characters in the string are digits

string_variable.isdigit()
Enter fullscreen modeExit fullscreen mode

islower() method
Returns True if all characters in the string are lower case

string_variable.islower()
Enter fullscreen modeExit fullscreen mode

isspace() method
Returns True if all characters in the string are whitespaces

string_variable.isspace()
Enter fullscreen modeExit fullscreen mode

isupper() method
Returns True if all characters in the string are upper case

string_variable.isupper()
Enter fullscreen modeExit fullscreen mode

lower() method
Converts a string into lower case equivalent

string_variable.lower()
Enter fullscreen modeExit fullscreen mode

upper() method
Converts a string into upper case equivalent

string_variable.upper()
Enter fullscreen modeExit fullscreen mode

strip() method
It removes leading and trailing spaces in the string

string_variable.strip()
Enter fullscreen modeExit fullscreen mode

List
A List in Python represents a list of comma-separated values of any data type between square brackets.

var_name = [element1, element2, ...]
Enter fullscreen modeExit fullscreen mode

index method
Returns the index of the first element with the specified value

list.index(element)
Enter fullscreen modeExit fullscreen mode

append method
Adds an element at the end of the list

list.append(element)
Enter fullscreen modeExit fullscreen mode

extend method
Add the elements of a given list (or any iterable) to the end of the current list

list.extend(iterable)
Enter fullscreen modeExit fullscreen mode

insert method
Adds an element at the specified position

list.insert(position, element)
Enter fullscreen modeExit fullscreen mode

pop method
Removes the element at the specified position and returns it

list.pop(position)
Enter fullscreen modeExit fullscreen mode

remove method
The remove() method removes the first occurrence of a given item from the list

list.remove(element)
Enter fullscreen modeExit fullscreen mode

clear method
Removes all the elements from the list

list.clear()
Enter fullscreen modeExit fullscreen mode

count method
Returns the number of elements with the specified value

list.count(value)
Enter fullscreen modeExit fullscreen mode

reverse method
Reverses the order of the list

list.reverse()
Enter fullscreen modeExit fullscreen mode

sort method
Sorts the list

list.sort(reverse=True|False)
Enter fullscreen modeExit fullscreen mode

Tuples
Tuples are represented as comma-separated values of any data type within parentheses.

Tuple Creation

variable_name = (element1, element2, ...)
Enter fullscreen modeExit fullscreen mode

Lets talk about some of the tuple methods:

count method
It returns the number of times a specified value occurs in a tuple

tuple.count(value)
Enter fullscreen modeExit fullscreen mode

index method
It searches the tuple for a specified value and returns the position.

tuple.index(value)
Enter fullscreen modeExit fullscreen mode

Sets
A set is a collection of multiple values which is both unordered and unindexed. It is written in curly brackets.

Set Creation: Way 1

var_name = {element1, element2, ...}
Enter fullscreen modeExit fullscreen mode

Set Creation: Way 2

var_name = set([element1, element2, ...])
Enter fullscreen modeExit fullscreen mode

Set Methods
Lets talk about some of the methods of sets:

add() method
Adds an element to a set

set.add(element)
Enter fullscreen modeExit fullscreen mode

clear() method
Remove all elements from a set

set.clear()
Enter fullscreen modeExit fullscreen mode

discard() method
Removes the specified item from the set

set.discard(value)
Enter fullscreen modeExit fullscreen mode

intersection() method
Returns intersection of two or more sets

set.intersection(set1, set2 ... etc)
Enter fullscreen modeExit fullscreen mode

issubset() method
Checks if a set is a subset of another set

set.issubset(set)
Enter fullscreen modeExit fullscreen mode

pop() method
Removes an element from the set

set.pop()
Enter fullscreen modeExit fullscreen mode

remove() method
Removes the specified element from the set

set.remove(item)
Enter fullscreen modeExit fullscreen mode

union() method
Returns the union of two or more sets

set.union(set1, set2...)
Enter fullscreen modeExit fullscreen mode

Python cheat sheet part-1

Top Websites to learn Python

How to install python in windows 10

Best IDE's for Python

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Developer 🧑‍🎓 Passionate about Web Development, Python Development and open-source. Book lover 📔 || Music Lover 🎶 || 💻 Internet surfer 🏄
  • Location
    Hyderabad, India
  • Education
    Master's in Computer Applications
  • Work
    Data Engineer
  • Joined

More fromSandeep

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp