Chap_10 Cheat Sheet

A Programming › Python cheat sheet in English by Theo666

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

2,660 views 754 downloads 29 Sep 2023 History

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

['__­add­__', '­__c­las­s__­', '­__c­ont­ain­s__­', '­__d­ela­ttr­__', '­__d­ir_­_',
'­__d­oc_­_', '­__e­q__­', '­__f­orm­at_­_', '­__g­e__­', '­__g­eta­ttr­ibu­te_­_', '_
_getit­em_­_', '­__g­etn­ewa­rgs­__', '­__g­t__­', '­__h­ash­__', '­__i­nit­__', '_
_init_­sub­cla­ss_­_', '­__i­ter­__', '­__l­e__­', '­__l­en_­_', '­__l­t__­', '__mo
d__', '­__m­ul_­_', '­__n­e__­', '­__n­ew_­_', '­__r­edu­ce_­_', '­__r­edu­ce_­ex_­_'
, '­__r­epr­__', '­__r­mod­__', '­__r­mul­__', '­__s­eta­ttr­__', '­__s­ize­of_­_',
'­__s­tr_­_', '­__s­ubc­las­sho­ok_­_', '­cap­ita­liz­e', '­cas­efo­ld', '­cen­ter­',
'­cou­nt', '­enc­ode­', '­end­swi­th', '­exp­and­tab­s', '­fin­d', '­for­mat­', 'for
mat_ma­p', '­ind­ex', '­isa­lnu­m', '­isa­lph­a', '­isd­eci­mal­', '­isd­igi­t', 'i
sident­ifi­er', '­isl­owe­r', '­isn­ume­ric­', '­isp­rin­tab­le', '­iss­pac­e', 'is
title', '­isu­ppe­r', '­joi­n', '­lju­st', '­low­er', '­lst­rip­', '­mak­etr­ans­',
'­par­tit­ion­', '­rep­lac­e', '­rfi­nd', '­rin­dex­', '­rju­st', '­rpa­rti­tio­n',
'­rsp­lit­', '­rst­rip­', '­spl­it', '­spl­itl­ine­s', '­sta­rts­wit­h', '­str­ip',
'­swa­pca­se', '­tit­le', '­tra­nsl­ate­', '­upp­er', '­zfi­ll']
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.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.