String in python | How to use string in python | Python Strings

String in python 

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:

str = ('Hello Python')
print (str)
print (str[0])
print (str[1:4])
print (str[4:])
print (str * 3)
print (str + "PYTHON")

Output:

Hello Python
H
ell
o Python
Hello PythonHello PythonHello Python
Hello PythonPYTHON


String Concatenation Operator (+) 

The concatenation operator (+) concatenates two Strings and creates a new String.

Example:

str = 'Hello'
str1 = 'Python'
print(str+str1)

Output:

HelloPython


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:

str = 'Hello'
print (str * 3)

Output:

HelloHelloHello


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:

str = 'Hello Python'
print (str[0])
print (str[1:4])
print (str[4:])

Output:

H
ell
o Python


strip() method 

The strip() method removes any whitespace from the beginning or the end. 

Example:

str = " Hello, Python "
print(str.strip())

Output:

Hello, Python


len() method 

The len() method returns the length of a string.

Example:

str = "Hello"
print(len(str))

Output:

5


lower() method 

The lower() method returns the string in lower case. 

Example:

str = "HELLO"
print(str.lower())

Output:

hello


upper() method 

The upper() method returns the string in upper case.

Example:

str = "hello"
print(str.upper())

Output:

HELLO


replace() method 

The replace() method replaces a string with another string. 

Example:

str = "Hello"
print(str.replace("H""i"))

Output:

iello


split() method 

The split() method splits the string into substrings if it finds instances of the separator

Example:

str = "Hello, Python"
print(str.split(","))

Output:

['Hello'' Python']


capitalize() Method 

This method capitalizes the first character of the String.

Example:

str = "hello, Python"
print(str.capitalize())

Output:

Hello, Python


isalnum() Method 

Returns true if string has at least 1 character and all characters are alphanumeric and false otherwise. 

Example:

str = "HelloPython3";
print(str.isalnum())
str = "Hello Python3";
print(str.isalnum())

Output:

True
False


isalpha() 

Returns true if string has at least 1 character and all characters are alphabetic and false otherwise.

Example:

str = "Hello";
print(str.isalpha())
str = "Hello Python";
print(str.isalpha())

Output:

True
False


isdigit() 

Returns true if string contains only digits and false otherwise

Example:

str = "Hello";
print(str.isdigit())
str = "123";
print(str.isdigit())

Output:

False
True


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. 

Example:

str = "Hello";
sub = "l";
print(str.count(sub, 14))

Output:

2

Next Post Previous Post
No Comment
Add Comment
comment url