1. Define a string in Python
In Python, a string is a sequence of characters enclosed within single quotes ('') or double quotes (""). It is one of the built-in data types in Python and is used to represent textual data. Strings are immutable! which means that once created, their content cannot be changed. However, you can perform various operations on strings to manipulate and extract information from them.
Here's an overview of some important characteristics and operations related to Python strings:
Declaration and Initialization:
You can declare and initialize a string variable by assigning a sequence of characters to it using quotes. For example:
1 2 |
message = 'Hello, World!' print(message) # output: 'Hello, World!' |
1 |
print(name_of_the_string_variable) |
Like many other popular programming languages, strings in Python are byte arrays representing Unicode characters. However, Python does not have a character (char) data type like char type in C, a single character is simply a string of length 1. Square brackets can be used to access string elements.
2. Length of a Python string
The length of a character string is by definition the number of characters that make up the string. To get the length of a string, we use the len() method.
Example: length of the string s = "Python"
1 2 3 |
s = "Python" print("The length of the string s is:", len(s)) # output: The length of the string s is: 6 |
3. Accessing the elements of a Python string
To access an element of a character string s, we use the syntax:
1 |
s[character_index] |
Example
Let's have fun displaying the first and second character of the string: s = "Python Programming"
(remember that the first character is at position 0):
1 2 3 4 5 |
s = "Python Programming" print("the first character of s is: ", s[0]) # displays: "the first character of s is 'P' print("the second character of s is: ", s[1]) # displays:"the second character of s is 'y' |
Example: displaying all characters of a string using the len() method
1 2 3 4 5 6 7 8 9 10 11 12 |
s="Python" for i in range(0 , len(s)): print(s[i]) # attach: """ P y t h o n """ |
Example: displaying total characters of a string via the iterator method
1 2 3 4 5 6 7 8 9 10 11 12 |
s="Python" for x in s: print(x) # output: """ P y t h o n """ |
4. Python String Operations
4.1 Concatenation of two Python string characters
To concatenate two string characters in python, we use the '+' operator:
Example
1 2 3 4 5 |
s1 = "Python" s2 = "Programming" # concatenation of s1 and s2 s = s1 + s2 print(s) # output: 'Python Programming' |
4.2 Extract a substring
We extract a substring of a string s from the ith position up to the jth not included using the syntax:
1 |
substring = string[i:j] |
Example
1 2 3 |
s = "Python" substring = s[1:4] print(substring) # output: 'yth' |
5. The main methods associated with character strings in Python
The Python language is equipped with a large number of functions allowing the manipulation of string characters: calculation of the length of the string, transformation into upper and lower case, extracting a sub-string... Here is a non-exhaustive list:
Here is a non-exhaustive list:
6. Example of using string functions
Example. transformation of a string into lower case
1 2 3 |
s = "CRMEF OUJDA" s = s.lower () print (s) # displays crmef oujda |
Example. replacing one occurrence with another
1 2 3 |
s = "CRMEF OUJDA" s = s.replace ("CRMEF", "ENS") print (s) # display: ENS OUJDA |
Example. Number of characters in a string
1 2 3 4 |
s = "CRMEF OUJDA" n = len(s) print ("the number of characters in the string s is:", n) # displays the number of characters in the string s is: 11 |
Example. String.format
1 2 3 4 |
name = "David" age = 37 s = 'Hello, {}, you have {} ans'.format (name, age) print (s) # display: 'Hello, David, you are 37' |
Example. extract a sub string
1 2 3 4 5 6 7 |
s = "CRMEF OUJDA" s1 = s [6: 9] print (s1) # displays OUJ s2 = s [6:] print (s2) # displays OUJDA s3 = s [: 4] print (s3) # displays CRME |
Younes Derfoufi
CRMEF OUJDA