Functions for handling Strings in Python - ProgrammerTech
Language:

Search

Functions for handling Strings in Python

  • Share this:
Functions for handling Strings in Python

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:

  1. The capitalize() function makes the first letter uppercase.
  2. The title() function makes the first character uppercase.
  3. The center() function puts something in place of spaces in the strings. In our example, we used asterisks.
  4. The len() function counts the number of characters in a word.
  5. The count() function counts similar characters in a sentence with the character selected.
  6. 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:

  1. index() function to find the position of a letter or word in the program.
  2. function isalnum() Does this specified variable have numbers? and returns true,false.
  3. function isalpha() Does this variable have letters? and returns true,false.
  4. The isdigit() function. Is the specified variable data numbers? and returns true,false.
  5. The lower() function increases the letters of the variable.
  6. upper() function to lower the characters of the variable.
  7. 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:

  1. The istitle() function Is the variable data first capitalized? and returns true,false.
  2. The title() function makes the first letter of the variable capitalized.
  3. The isspace() function asks for a space as in the example Is this myspace? and returns true,false.
  4. The lstrip() function deletes characters from the sentence from the left while selecting the character.
  5. The rstrip() function deletes characters from the sentence from the right while selecting the character.
  6. The strip() function removes from both directions, right and left, with selected characters.
  7. The split() function separates the words.

Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com