String Methods
Published: 2023-02-16 04:27:34

There are three different types of strings in python. They are 

- string literals
- raw strings
- formatted strings

**String literals** - String literals can be defined using single quotes `'`, double quotes `"` and triple quotes `"""`. The escape characters are mentioned inside using a backslash `\` for example a new line would be `\n`.

**Raw strings** - In raw string a backslash is treated as a literal character. A string is treated as a raw string of its prefixed with an `r` in front. Raw strings are used for regular expressions.

**Formatted strings** - Also called f-strings allows us to insert values of variables into a string. The value of the variable or expression inside curly braces will replace the curly brace section inside the f-string. Placing the semicolon `:` after the variable inside the curly brace will help change the format specification.

### String concatenation

String concatenation can be done by both the `+` operator and the join function and f-string. In terms of performance, the f-string is the fastest one, followed by the join and plus operators the last.

## Common String methods


**capitalize**() - capitalizes the first character of a string and all others in lowercase.

```python
"first Second".capitalize() # returns "First second"
```
**center**(width[, fillchar]) - returns a string in the center within a specified width.

```python
"word".center(10, "-") # returns "---word---"
```

**count**(sub[, start[, end]]) - returns the number of occurrences of a substring within a string.

```python
"hello".count("l") # returns 2
```

**find**(sub[, start[, end]]) - returns the lowest index of the substring within the string.

```python
"hello world".find("o") # returns 4
```

**format**(*args, **kwargs) - performs string formatting operation.

```python
"Hi {}, It's {} now.".format("name", 1 + 1) # returns "Hi name, It's 2 now."
```

**format_map**(mapping) - formats a string using a dictionary.

```python
"Hi {name}, It's {time} now.".format_map({"name": "Batman", "time": 12}) # returns "Hi Batman, It's 12 now."
```

**index**(sub[, start[, end]]) - returns the lowest index of the substring within the string.

```python
"hello".index("o") # returns 4
```

**isalnum**() - returns True if all characters in the string are alphanumeric.

```python
"a1".isalnum() # returns True
```

**isalpha**() - returns True if all characters in the string are alphabetic.

```python
"hello".isalpha() # returns True
```

**isdecimal**() - returns True if all characters in the string are decimal. Decimal characters are those that can be used to form numbers in base 10.

```python
 "123".isdecimal() # returns True
```

**isdigit**() - returns True if all characters in the string are digits. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits.

```python
"¹²³".isdigit() # returns True
```

**isnumeric**() - returns True if all characters in the string are numeric. Numeric characters include digit characters, and all characters that have the Unicode numeric value property.

```python
"ⅠⅩⅤⅬⅭⅮⅯ".isnumeric() # returns True
```

**islower**() - returns True if all characters in the string are lowercase.

```python
"hello".islower() # returns True
```

**isupper**() - returns True if all characters in the string are uppercase.

```python
"HELLO".isupper() # returns True
```

**join**(iterable) - returns a string concatenated with the elements of an iterable.

```python
" ".join(["Hello", "World"]) # returns "Hello World"
```

**lower**() - converts the string to lowercase.

```python
"Hello".lower() # returns "hello"
```

**replace**(old, new[, count]) - returns a string with all occurrences of the old string replaced with the new string.

```python
"Hi First".replace("First", "New") # returns "Hello New"
```

**split**([sep[, maxsplit]]) - returns a list of the words in the string.

```python
"Hello World".split() # returns ["Hello", "World"]
```

**strip**() - returns string with leading and trailing whitespace removed.

```python
"hello  ".strip() # returns "hello"
```

**upper**() - converts the string to uppercase.

```python
"hello".upper() # returns "HELLO"
```