string methods
Extended Slicing and String Methods
Extended Slicing
Syntax:
variable[start_index:end_index:step]
Step determines the increment between each index for slicing.
Code
PYTHON
Output
Methods
Python has a set of built-in reusable utilities. They simplify the most commonly performed operations are:
String Methods
- isdigit()
- strip()
- lower()
- upper()
- startswith()
- endswith()
- replace()and more...
Isdigit
Syntax:
str_var.isdigit()
Gives
True
if all the characters are digits. Otherwise, False
Code
PYTHON
Output
Strip
Syntax:
str_var.strip()
Removes all the leading and trailing spaces from a string.
Code
PYTHON
Output
Strip - Specific characters
Syntax:
str_var.strip(chars)
We can also specify characters that need to be removed.
Code
PYTHON
Output
Strip - Multiple Characters
Removes all spaces, comma(,) and full stop(.) that lead or trail the string.
Code
PYTHON
Output
Replace
Syntax:
str_var.replace(old,new)
Gives a new string after replacing all the occurrences of the old substring with the new substring.
Code
PYTHON
Output
Startswith
Syntax:
str_var.startswith(value)
Gives
True
if the string starts with the specified value. Otherwise, False
Code
PYTHON
Output
Endswith
Syntax:
str_var.endswith(value)
Gives
True
if the string ends with the specified value. Otherwise, False
Code
PYTHON
Output
Upper
Syntax:
str_var.upper()
Gives a new string by converting each character of the given string to uppercase.
Code
PYTHON
Output
Lower
Syntax:
str_var.lower()
Gives a new string by converting each character of the given string to lowercase.
Code
PYTHON
Comments
Post a Comment