From e1d163928e27b6aa1594f92b0f069cd311838f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Mon, 6 May 2019 12:30:42 +0200 Subject: [PATCH] String --- README.md | 11 ++++++++--- index.html | 12 +++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b1257d8..64b14ae 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ list_of_chars = list() ``` ```python -index = .index() # Returns first index of item. +index = .index() # Returns first index of item or raises ValueError. .insert(index, ) # Inserts item at index and moves the rest to the right. = .pop([index]) # Removes and returns item at index or from the end. .remove() # Removes first occurrence of item or raises ValueError. @@ -269,8 +269,9 @@ String ``` ```python - = .split() # Splits on any whitespace character. + = .split() # Splits on one or more whitespace characters. = .split(sep=None, maxsplit=-1) # Splits on 'sep' str at most 'maxsplit' times. + = .splitlines(keepends=False) # Splits on line breaks. Keeps them if 'keepends'. = .join() # Joins elements using string as separator. ``` @@ -278,7 +279,8 @@ String = .replace(old, new [, count]) # Replaces 'old' with 'new' at most 'count' times. = .startswith() # Pass tuple of strings for multiple options. = .endswith() # Pass tuple of strings for multiple options. - = .index() # Returns start index of first match. + = .find() # Returns start index of first match or -1. + = .index() # Same but raises ValueError. ``` ```python @@ -286,6 +288,9 @@ String = textwrap.wrap(, width) # Nicely breaks string into lines. ``` +* **Also: `'lstrip()'`, `'rstrip()'`.** +* **Also: `'lower()'`, `'upper()'`, `'capitalize()'` and `'title()'`.** + ### Char ```python = chr() # Converts int to unicode char. diff --git a/index.html b/index.html index b630251..e639f46 100644 --- a/index.html +++ b/index.html @@ -237,7 +237,7 @@ flatter_list = list(itertools.chain.from_iterable(<list>)) product_of_elems = functools.reduce(lambda out, x: out * x, <collection>) list_of_chars = list(<str>) -
index = <list>.index(<el>)     # Returns first index of item.
+
index = <list>.index(<el>)     # Returns first index of item or raises ValueError.
 <list>.insert(index, <el>)     # Inserts item at index and moves the rest to the right.
 <el> = <list>.pop([index])     # Removes and returns item at index or from the end.
 <list>.remove(<el>)            # Removes first occurrence of item or raises ValueError.
@@ -377,18 +377,24 @@ Point(x=1, y=2
 
<str>  = <str>.strip()                       # Strips all whitespace characters from both ends.
 <str>  = <str>.strip('<chars>')              # Strips all passed characters from both ends.
 
-
<list> = <str>.split()                       # Splits on any whitespace character.
+
<list> = <str>.split()                       # Splits on one or more whitespace characters.
 <list> = <str>.split(sep=None, maxsplit=-1)  # Splits on 'sep' str at most 'maxsplit' times.
+<list> = <str>.splitlines(keepends=False)    # Splits on line breaks. Keeps them if 'keepends'.
 <str>  = <str>.join(<collection>)            # Joins elements using string as separator.
 
<str>  = <str>.replace(old, new [, count])   # Replaces 'old' with 'new' at most 'count' times.
 <bool> = <str>.startswith(<sub_str>)         # Pass tuple of strings for multiple options.
 <bool> = <str>.endswith(<sub_str>)           # Pass tuple of strings for multiple options.
-<int>  = <str>.index(<sub_str>)              # Returns start index of first match.
+<int>  = <str>.find(<sub_str>)               # Returns start index of first match or -1.
+<int>  = <str>.index(<sub_str>)              # Same but raises ValueError.
 
<bool> = <str>.isnumeric()                   # True if str contains only numeric characters.
 <list> = textwrap.wrap(<str>, width)         # Nicely breaks string into lines.
 
+
    +
  • Also: 'lstrip()', 'rstrip()'.
  • +
  • Also: 'lower()', 'upper()', 'capitalize()' and 'title()'.
  • +

Char

<str> = chr(<int>)  # Converts int to unicode char.
 <int> = ord(<str>)  # Converts unicode char to int.