<commit_before>---
doc: https://docs.python.org/3.4/library/string.html
---

```python
# Simple string manipulations
text.lower()
text.upper()
text.isalpha()  # Returns True if it only has letters
text.isdigit()  # Returns True if it only has numbers
text.isalnum()  # Returns True if it has letters or numbers
text.startswith('abc')

len(text)

# Replace
# (For regex, use its package)
text.replace('pattern', repl, count=0)

# Split
text.split()  # Default is ' '
text.splitlines()

# Strip
text.strip()  # Default is ' '
text.rstrip()
text.lstrip()

# String addtion
text_sum = 'Hello ' + 'world!'

# Unicode support
# coding:utf-8
mystring = u'こんにちは'
mystring = 'こんにちは'.decode('utf-8')

import unicodedata
mystring = unicodedata.normalize('NFC', u'こんにちは')
```

## Format (new style)

```python
# Basic usage
'Hello {}, there is a {:#x} error!'.format('Bob', 50159747054)
'Hello {name}, there is a {errno:#x} error!'.format(name='Bob', errno=50159747054)

# Add 0 to left of a number
'{:03d}'.format(8)  # 008

# Decimal number with fixed decimal places
'{:.4f}'.format(3.141592653589793)  # 3.1416
```
<commit_msg>Update python string format notes<commit_after>---
doc: https://docs.python.org/3/library/string.html
---

```python
# Simple string manipulations
text.lower()
text.upper()
text.isalpha()  # Returns True if it only has letters
text.isdigit()  # Returns True if it only has numbers
text.isalnum()  # Returns True if it has letters or numbers
text.startswith('abc')

len(text)

# Replace
# (For regex, use its package)
text.replace('pattern', repl, count=0)

# Split
text.split()  # Default is ' '
text.splitlines()

# Strip
text.strip()  # Default is ' '
text.rstrip()
text.lstrip()

# String addtion
text_sum = 'Hello ' + 'world!'

# Unicode support
# coding:utf-8
mystring = u'こんにちは'
mystring = 'こんにちは'.decode('utf-8')

import unicodedata
mystring = unicodedata.normalize('NFC', u'こんにちは')
```

## Format

Array-like usage:

```python
'{}{}{}'.format(a, b, c)
'{0}{1}{2}'.format(a, b, c)
```

Dictionary-like usage:

```python
'{name}'.format(name='John')
```

Numbers formatting:

```python
'{:03d}'.format(8)  # '008'
'{:+.4f}'.format(3.141592653589793)  # '+3.1416'
'{:-.4f}'.format(3.141592653589793)  # '3.1416'
'{: .4f}'.format(3.141592653589793)  # ' 3.1416'
'{:.2%}'.format(0.2548)  # '25.48%'
```

Content alignment:

```python
'{:<10}'.format('left')    # 'left      '
'{:^10}'.format('center')  # '  center  '
'{:>10}'.format('right')   # '     right'
'{:_^10}'.format('fill')   # '___fill___'
```

{'fraction_non_alphanumeric': 0.1998417095370004, 'fraction_numerical': 0.060546102097348634, 'mean_word_length': 4.436559139784946, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1860216', 'n_tokens_mistral': 1108, 'n_tokens_neox': 988, 'n_words': 226}