Handling Strings in Python
Strings are a set of tasks that deal with texts, for example, we have text data that we want to deal with and control, whether in form or format, so we must use these methods, and without focusing on this lesson, you will not understand the rest of the upcoming lessons because everything is related to each other We recommend reading it several times.
Examples of functions in the str class
Below we will explain several examples of some of the functions that we use frequently with the str text class, with an explanation of these examples and the meanings of the functions.
Example 1
>>> name = "programmer"
>>> name
'programmer'
>>> name.capitalize()
'Programmer'
>>> name.title()
'Programmer'
>>> name.center(11, '*')
'*programmer'
>>> name.center (16, '*')
'***programmer***'
>>> len (name)
10
>>> name.count('m',0,len (name))
2
>>> name.endswith('er', 0)
True
whereas:
- The capitalize() function makes the first letter uppercase.
- The title() function makes the first character uppercase.
- The center() function puts something in place of spaces in the strings. In our example, we used asterisks.
- The len() function counts the number of characters in a word.
- The count() function counts similar characters in a sentence with the character selected.
- The function endswith() Does the variable end with a letter and jokes it letters and returns us with true , false .
Example 2
>>> name = "programmer"
>>> name.index ('g', 0, len(name))
3
>>> data = "6445156"
>>> data.isalnum()
True
>>> data2 = "ProgrammerTech"
>>> data2.isalpha()
True
>>> data3 = "0249"
>>> data3.isdigit()
True
>>> data4 = "KAISSAR"
>>> data4.lower()
'kaissar'
>>> 'kaissar' .upper()
'KAISSAR'
>>> data4.isupper()
True
whereas:
- index() function to find the position of a letter or word in the program.
- function isalnum() Does this specified variable have numbers? and returns true,false.
- function isalpha() Does this variable have letters? and returns true,false.
- The isdigit() function. Is the specified variable data numbers? and returns true,false.
- The lower() function increases the letters of the variable.
- upper() function to lower the characters of the variable.
- isupper() Is the variable uppercase? and returns true,false.
Example 3
>>> name = "programmer"
>>> data4 = "KAISSAR"
>>> data4.istitle()
False
>>> data4.title()
'Kaissar'
>>> sp = " "
>>> sp.isspace()
True
>>> data5 = "hello world"
>>> data5.lstrip('h')
'ello world'
>>> data5.rstrip('d')
'hello worl'
>>> data5.strip('d')
'hello worl'
>>> data6 = "programmer tech"
>>> data6.split(" ")
['programmer', 'tech']
whereas:
- The istitle() function Is the variable data first capitalized? and returns true,false.
- The title() function makes the first letter of the variable capitalized.
- The isspace() function asks for a space as in the example Is this myspace? and returns true,false.
- The lstrip() function deletes characters from the sentence from the left while selecting the character.
- The rstrip() function deletes characters from the sentence from the right while selecting the character.
- The strip() function removes from both directions, right and left, with selected characters.
- The split() function separates the words.