1. Define a string in Python
Like all other programming languages, strings in python is a sequence of Unicode characters taken as a Python variable in the form of expressions surrounded by single quotes or double quotes. For example "Python Programming" is a Python string identical to 'Python Programming'.
Python Strings can be displayed on screen using the print() function:
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"
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:
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):
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
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
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
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:
substring = string[i:j]
Example
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
s = "CRMEF OUJDA" s = s.lower () print (s) # displays crmef oujda
Example. replacing one occurrence with another
s = "CRMEF OUJDA" s = s.replace ("CRMEF", "ENS") print (s) # display: ENS OUJDA
Example. Number of characters in a string
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
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
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
my-courses.net