In this tutorial, we will see Python Strings.
What is String?
The string is a collection of alphabets. The string is one of the data types in python. In python, String is immutable, which means the string is not changeable.
How to create a string in Python
There are two types of strings in python.
- Single Line String
- Multiple Line Strings
Single Line Strings: How to create a single-line string in python
A single line string is a string that consists of a single line(one line). Single line strings are denoted using a double quotation or a single quotation.
Examples. “python strings”, ‘Cyber Fauz’, etc.
- Single Quotation can be used inside the double quotation, as well as double quotation, can be used inside the single quotation.
- Single Quotation can not be used inside a single quotation.
- The double quotation can not be used inside the double quotation.
Example: Single Line String
#Example 1 a = "Hello, World!" #single line string b = "Cyber Fauz - Learning Site" c = 'Python Strings' #printing All the Strings print(a) # Output: Hello, World! print(b) # Output: Cyber Fauz - Learning Site print(c) # Output: Python Strings
Multiple Line Strings: How to create a multiple-line string in python
A multiple-line string is a string that consists of multiple lines (more than one line). Multiple line strings are denoted using three double quotations or three single quotations.
Example: Multiple Line String
#Example 2 text = """ Python is a high level programming language. """ print(text)
#Example 3 text = ''' We can make good graphical user interface using Tkinter. ''' print(text)
How to access an element in a string
If you want to access a single element from a string, Indexing can be used to access a single element. If you want to access the group of characters, then slicing will be used.
Here we will look at Indexing and slicing one by one.
Indexing
Every element in every string has a value, which we call the index value. With the help of indexing, we can access a single element from any string.
There are two types of indexing in Python.
- Positive Indexing
- Negative Indexing
The positive indexing starts with 0 and the negative indexing starts with -1 from the backward direction.
Example: Indexing
#Example 4 a = "cyber fauz" print(a[0]) #output: c print(a[1]) #output: y print(a[-1]) #output: z print(a[-2]) #output: u print(a[10]) #IndexError: string index out of range
Slicing
If you want to access a particular part from a string or want to access the group of characters, then slicing will be used.
Slicing Syntax(Rules)
Example: Slicing
#Example 5 a = "cyber fauz" print(a[0:5]) #Output: 'cyber' print(a[6: ]) #Output: 'fauz' print(a[ :4]) #Output: 'r fauz' print(a[0:9:2]) #Output: 'cbrfu'
#Example 6 b = "python programming" print(b[0:6]) #Output: 'python' print(b[7: ]) #Output: 'programming'
Properties of Python String
- In Python, the String is immutable which means the string is not changeable.
#Example 7 m = "cyber fauz" m[6] = "Z" #Error #TypeError: 'str' object does not support item assignment
- Concatenating a string to another string is called string concatenation. In string concatenation, “+” operator is used.
#Example 8 a = 'cyber' b = 'fauz' print(a + b) #Output: 'cyberfauz' print(a + " " + b) #Output: 'cyber fauz'
- To check the particular string is a subpart of another string, we can use in operator (membership operator).
#Example 9 a = 'python programming' #To check 'python' is present or not in variable a print('python' in a) #Output: True #To check 'java' is present or not in variable a print('java' in a) #Output: False
- Comparison of string: The == , != operator (Comparison operator) is used to compare the string.
#Example 10 a = 'python' b = 'python' c = 'java' print(a == b) #Output: True print(a != b) #Output: False print(a == c) #Output: True print(a != c) #Output: False
Find the length of the string – len() function
The len() function is used to find the length of the string. len() is one of the built-in functions in python.
Example : len() Function
#Example 11 a = "python" #Length of the variable a print(len(a)) #Output: 6
#Example 12 b = "programming" #Length of the variable b print(len(b)) #Output: 11
String Methods
Python has many built-in methods. Which can be used for different purposes.
Method Name | Description | More |
---|---|---|
upper() | Converts the string into uppercase. | Know More |
lower() | Converts the string into lowercase. | Know More |
capitalize() | Converts the string first character to uppercase. | Know More |
title() | Converts the string first character of every word to uppercase. | Know More |
swapcase() | Converts uppercase character to lowercase and lowercase character to uppercase. | Know More |
isalpha() | Checks all the characters in the string are alphabets or not. | Know More |
isupper() | Checks all the characters in the string are uppercase alphabets(A-Z) or not. | Know More |
islower() | Checks all the characters in the string are lowercase alphabets(a-z) or not. | Know More |
isalnum() | Checks all the characters in the string are alphabets(A-Z , a-z) or Digit(0-9). | Know More |
isdigit() | Checks all the characters in the string are digits(0-9) or not. | Know More |
startswith() | Checks whether the string is starting with a particular string(prefix) or not. | Know More |
endswith() | Checks whether the string is starting with a particular string(prefix) or not. | Know More |
replace() | Used to replace a substring in a string with another substring. | Know More |
split() | Used to break a string. | Know More |
join() | Used to join or combine strings into one string. | Know More |
find() | Used to find the position of the value. | Know More |
Check your knowledge: Python Quizzes