String Methods
Tags: python
Published: 2023-02-16 04:05:29

Strings in python are enclosed in single quotes('..') or double quotes("..").
```{python}
print("Hello World!")
print('Python')
```
You can use three single quotes or three double quotes to assign multi line string to a variable.
```{python}
var = '''The output string is 
enclosed in quotes and 
special characters are escaped with backslashes.'''
print(var)
``` 
+ String Length **len()**

To get the length of a string, use the len() function.
```{python}
print(len(var))
```
+ **split()**


To get a list of substrings from the given string we use split() function.

Syntax: **str.split(separator, maxsplit)**
```{python}
str1 = "Hello World"
print(str1.split())
```
If we don't specify any separator it takes space as default value.


Maxsplit is used to specify number of splits we need and it's default value is -1.

+ **join()**

To join a list of substrings into a single string we use join() function.

Syntax: **str.join(iterable)**
```{python}
list_1 = ['Hello','World!']
string1 = " ".join(list_1)
```
+ **index()**

To get the index of a substring we use index() function.
```{python}
str1 = 'Python Programming'
print(str1.index('i'))
```
It raises an ValueError exception if substring is not found.

+ **lower()**

Converts all uppercase letters in string to lowercase.
```{python}
str1 = 'Python Programming'
print(str1.lower())
```

+ **upper()**

Converts all lowercase letters in string to uppercase.
```{python}
str1 = 'Python Programming'
print(str1.upper())
```

+ **islower()**

Returns True only when all the characters in string are in lowercase and False otherwise.
```{python}
str1 = 'Python Programming'
print(str1.islower())
```


+ **isupper()**

Returns True only when all the characters in string are in uppercase and False otherwise.
```{python}
str1 = 'Python Programming'
print(str1.isupper())
```


+ **isdigit()**

Returns True only when all the characters in string are digits and False otherwise.
```{python}
str1 = '123'
print(str1.isdigit())
```


+ **isalpha()**

Returns True only when all the characters in string are alphabetic and False otherwise.
```{python}
str1 = 'Python Programming'
print(str1.isalpha())
```

+ **capitalize()**

Converts the first character in string to upper case and all other characters to lowercase.
```{python}
str1 = 'python'
print(str1.capitalize())
```

+ **casefold()**

Returns a copy of string in lowercase.
```{python}
str1 = 'Python Programming'
print(str1.casefold())
```

+ **endswith()**

Returns True if the string ends with the specified suffix, otherwise False.
```{python}
str1 = 'Python Programming'
print(str1.endswith('ing'))
```

+ **startswith()**

Returns True if the string starts with the specified prefix, otherwise False.
```{python}
str1 = 'Python Programming'
print(str1.startswith('Pyth'))
```

+ **swapcase()**

Returns a string with uppercase characters converted to lowercase and vice versa.
```{python}
str1 = 'pYTHON pROGRAMMING'
print(str1.swapcase())
```
+ **strip()**

Returns a copy of the string with leading and trailing characters removed.


Syntax: **str.strip([chars])**


The chars argument is a string specifying the set of characters to be removed.


By default chars set to remove whitespace.
```{python}
str1 = '  Python   '
print(str1.strip())
```

+ **replace()**

Syntax: **str.replace(old, new)**

Returns a copy of the string with all occurrences of old substring replaces by new substring.
```{python}
str1 = 'Python Programming'
print(str1.replace('Python', 'Java'))
```


**Reference:**
[String Methods](https://docs.python.org/3/library/stdtypes.html#string-methods)