PYTHON : STRING METHODS
Tags: python, string, methods
Published: 2023-02-16 04:10:14


### capitalize()
* It returns the string in the form of first character is upper case and the rest in lower case.

#### Syntax
```
string.capitalize()
```
#### Example
```
txt = "hi, welcome!"
a = txt.capitalize()
print(a)
#output:Hi, welcome!
```

### casefold()
* It returns the string where all the characters are lower case.

#### Syntax
```
string.casefold()
```
#### Example
```
txt = "This Is My Example"
x = txt.casefold()
print(x)
#output: this is my example
```

### center()
* It is a string class function that is used to position a string by padding it with a specified character.

#### Syntax
```
string.center(length, character)
#length - length of the required string.
#character - to fill the missing space on each side and default is " "(space).
```
#### Example
```
txt = "banana"
x = txt.center(20, "x")
print(x)
#output: xxxxxxxbananaxxxxxxx
```

### count()
* This method returns the number of times a specified value appears in the string.

#### Syntax
```
string.count(value, start, end)
#value - value to search for a string
#start - position to start the search and default is 0
#end - position to end the search and default is end of the string
```
#### Example
```
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple", 10, 24)
print(x)
#output: 1
```

### endswith()
* This method method returns True if the string ends with the specified value, otherwise False.

#### Syntax
```
string.endswith(value, start, end)
```

#### Example
```
txt = "Hello, welcome to my world."
x = txt.endswith("!")
print(x)
#output:False
```

### expandtabs()
* This method sets the tab size to the specified number of whitespaces.

#### Syntax
```
string.expandtabs(tabsize)
#tabsize - a number specifying the tabsize and default is 8.
```

#### Example
```
txt = "W\te\tl\tc\to\tm\te"
print(txt.expandtabs(1)) 
#output: W e l c o m e
```

### find()
* It finds the first occurrence of the specified value.
* It will returns -1 if the value not found.
* This method is almost same as index() method, the only difference is that the index() method rasie an exception if the value is not found.

#### Syntax
```
string.find(value, start, end)
```

#### Example
```
txt = "Hello, welcome to my world."
x = txt.find("n")
print(x)
#output: -1
```

### format()
* This method formats the specified value or values and insert them inside the string placeholder.
* the placeholder is defined using curly brackets {}.

#### Syntax
```
string.format(value1, value2,....)
# values can required one or more values . 
```

#### Example
```
txt = "For only {price:.2f} dollars!"
print(txt.format(price = 49))
#output: For only 49.00 dollars! 
```

### index()
* This method finds the first occurrence of the specified value.
* It raises an exception if the value is not found.

#### Syntax
```
string.index(value, start, end)
```

#### Example
```
txt = "Hello, welcome to my world."
x = txt.index("m")
print(x)
#ouput: 12
```

### isalnum()
* This method returns true if all the characters are alphanumeric ,means letter(a-z) and numbers (0-9).

#### Syntax
```
string.isalnum()
```

#### Example
```
txt = "company123"
x = txt.isalnum()
print(x)
#output: True
```

### isalpha()
* It returns true if all the characters are alphabet letters(a-z).

#### Syntax
```
string.isalpha()
```

#### Example
```
txt = "company123"
x = txt.isalpha()
print(x)
#output: False
```

### isascii()
* It returns True if all the characters are ascii characters(a-z).

#### Syntax
```
string.isascii()
```

#### Example
```
txt = "company123"
x = txt.isascii()
print(x)
#output: true
```

### isdecimal()
* It returns True if all the characters are decimanls(0-9).

#### Syntax
```
string.isdecimal()
```

#### Example
```
a = "\u0030"
print(a.isdecimal())
#ouput: True
```

### swapcase()
* It returns a string where all the upper case letters to lower case and vise versa.

#### Syntax
```
string.swapcase()
```

#### Example
```
txt = "Hello My Name Is PETER"
x = txt.swapcase()
print(x) 
#output:hELLO mY nAME iS peter 
```

### join()
* It takes all items in an iterable and joins them into one string.

#### Syntax
```
string.join(iterable)
```

#### Example
```
myDict = {"name": "John", "country": "Norway"}
mySeparator = "TEST"
x = mySeparator.join(myDict)
print(x)
#ouput: nameTESTcountry 
```

### split()
* It splits into a list.

#### Syntax
```
string.split(separator, maxsplit)
#separator - use when splitting the string and by default any whitespace is a separator
# maxsplit - how many splits to do and default value is -1, which is "all occurrences"
```

#### Example
```
txt = "apple#banana#cherry#orange"
x = txt.split("#")
print(x)
#output: ['apple', 'banana', 'cherry', 'orange']
```

### zfill()
* It adds zeros at the beginning of the string, until it reaches the specified length.

#### Syntax
```
string.zfill(len)
```

#### Example
```
txt = "50"
x = txt.zfill(10)
print(x)
#output: 0000000050
```

### upper()
* It returns a string where all characters are in upper case.

#### Syntax
```
string.upper()
```

#### Example
```
txt = "hello"
a = txt.upper()
print(a)
# output: HELLO
```