String in python | How to use string in python | Python Strings
String in python
The string can be defined as the sequence of characters represented in the quotation marks. In python, we can use single, double, or triple quotes to define a string. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 at the beginning of the string and working their way from -1 at the end. The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator
Example:
Output:
String Concatenation Operator (+)
The concatenation operator (+) concatenates two Strings and creates a new String.
Example:
Output:
String Replication Operator (*)
The Replication operator is used to repeat a string number of times. The string will be repeated the number of times which is given by the integer value.
Example:
Output:
String Slice Notation
Python String slice can be defined as a substring that is the part of the string. Therefore further substring can be obtained from a string. There can be many forms to slice a string, a string can be accessed or indexed from both the direction and hence string can also, be sliced from both directions.
Example:
Output:
strip() method
The strip() method removes any whitespace from the beginning or the end.
Example:
Output:
len() method
The len() method returns the length of a string.
Example:
Output:
lower() method
The lower() method returns the string in lower case.
Example:
Output:
upper() method
The upper() method returns the string in upper case.
Example:
Output:
replace() method
The replace() method replaces a string with another string.
Example:
Output:
split() method
The split() method splits the string into substrings if it finds instances of the separator
Example:
Output:
capitalize() Method
This method capitalizes the first character of the String.
Example:
Output:
isalnum() Method
Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise.
Example:
Output:
isalpha()
Returns true if string has at least 1 character and all characters are alphabetic and false otherwise.
Example:
Output:
isdigit()
Returns true if string contains only digits and false otherwise
Example:
Output:
count() Method
The method count() returns the number of occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.