Python:String
lastmod 2018-10-03created 2015-03-0117 edits
Notes on Python strings.
String literals
- 2.4.1 String literals
- Regular expressions: python re r' (raw string)
- Stackoverflow: What exactly do “u” and “r” string flags do in Python, and what are raw string literals?
The r prefix makes a raw string, which leaves backslashes uninterpreted, so it is useful for things like regular expressions. For example, without the r prefix you would have to write
which is long and inconvenient because every backslash must be doubled. That is why the r prefix is usually added.
Functions
-
split(param1): Splits a string. Splits on theparam1string. -
find: Finds a string. -
rfind: Finds a string in reverse order. -
strip: Removes leading and trailing whitespace (Trim). -
replace: Replaces a string.
Replace
Use Regexp
Format string
Format strings are applied as follows.
Or
Insert string
Insert a string as follows.
def insert_dash(string, index):
return string[:index] + '-' + string[index:]
print insert_dash("355879ACB6", 5)
Multiline strings
To spread a string across multiple lines, use the following.
Or
Or
Or
Favorite site
- [Recommended] Jump to Python: String 1
- Yeolhyeol Gangui Python: Strings
- Tutorials Point: Python Strings
References
-
Jump_to_Python_-_String.pdf ↩
Pages that link here