Show Menu
Cheatography

strings in Python are iterable, indexable and immutable (id)

String

strings in Python are iterable, indexable and immutable (id)
len(st­ring)
string­[st­art­:en­d:step] step can be negative

Conversion between string and list

str()
convert an object to a string
string.sp­lit­("se­p"; maxsplit = )
split a string into a list of substr­ings. By default, it uses whitespace (spaces, tabs, and newlines) as the delimiter
"­sep­".jo­in(­object)
concat­enate the elements of an iterable object into a string
- join() : If use join on a list with only numeric elements, TypeError because the + operator is not defined for combining a list and an integer

other functions

['__ad­d__', '__cla­ss__', '__con­tai­ns__', '__del­att­r__', '__dir__',
'__doc__', '__eq__', '__for­mat__', '__ge__', '__get­att­rib­ute__', '_
_getit­em__', '__get­new­arg­s__', '__gt__', '__has­h__', '__ini­t__', '_
_init_­sub­cla­ss__', '__ite­r__', '__le__', '__len__', '__lt__', '__mo
d__', '__mul__', '__ne__', '__new__', '__red­uce__', '__red­uce­_ex__'
, '__rep­r__', '__rmo­d__', '__rmu­l__', '__set­att­r__', '__siz­eof__',
'__str__', '__sub­cla­ssh­ook__', 'capit­alize', 'casef­old', 'center',
'count', 'encode', 'endsw­ith', 'expan­dtabs', 'find', 'format', 'for
mat_map', 'index', 'isalnum', 'isalpha', 'isdec­imal', 'isdigit', 'i
sident­ifier', 'islower', 'isnum­eric', 'ispri­nta­ble', 'isspace', 'is
title', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maket­rans',
'parti­tion', 'replace', 'rfind', 'rindex', 'rjust', 'rpart­ition',
'rsplit', 'rstrip', 'split', 'split­lines', 'start­swith', 'strip',
'swapc­ase', 'title', 'trans­late', 'upper', 'zfill']
The dir() function in Python returns all of the object’s properties and methods, but not their values1. When you call dir(st­ring), it returns a list of valid attributes and methods that can be used with the string object.

Operation in the stings

string + string
string * int
same to list, if string multipled by negative numbers or zero, it returns a empty string. if string multipled by floats, error
 

Common Functions

str.pl­ace(a, b)
replace a by b in a string
str.co­unt(a)
count thte number of a in a string
str.ti­tle()
str.up­per()
str.lo­wer()
str.st­rip­("ch­ara­cte­r")
remove leading and trailing specific charaters. By default, whites­paces (spaces, tab, newline)
str.rs­tri­p("c­har­act­er")
remove trailing specific character. By default, whites­paces
str.ls­tri­p("c­har­act­er")
remove leading specific charac­hter. By default, whites­paces
str.is­alpha()
check whether all the characters in a string are alphabeti. return True or False
str.is­num­eric()
check whether all the characters in a string are numeric
str.is­dec­imal()
check whether all characters in a string are decimal digits (0-9)
str.fi­nf(­"­to_­fin­d")
find the first occurrence of a specified substring within a string. It returns the index of the first character of the substring if found, and -1 if the substring is not present in the string
str.st­art­wit­h("...")
isnume­ric() checks for a broader range of numeric charac­ters, including those from other scripts, while isdeci­mal() only recognizes decimal digits. Neither method recognizes decimal numbers or fractions.

Espace Sequence

\n
\\
\t
\"
\'
help us differ­­en­tiate between characters that have a special meaning in the progra­­mming language and characters that are part of the string itself. If a string contain \n in the text, \\n to distin­guish newline charchater and text \n. Except for \' and \", we can also use single­/double quote to distin­guish quote in text. \r idem chapter 7
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Studying Cheat Sheet
          Python_Chap_4 Cheat Sheet

          More Cheat Sheets by Theo666

          Python_Chap_2 Cheat Sheet
          Python_Chap_4 Cheat Sheet
          Chap_3 Cheat Sheet