What are str objects in Python

Table of contents:

What are str objects in Python
What are str objects in Python
Anonim

The name str in Python is used to refer to strings. It is a built-in data type that represents ordered sequences of Unicode characters. Typically, strings contain textual information. They are similar to C arrays, but have a number of powerful processing features.

Image
Image

String literals

There are several ways to write strings. The most popular are quotation marks and apostrophes. They are interchangeable and use them to eliminate the backslash character "\":

>>>Example_1="This is how lines are written"

Three quotes are also allowed when processing strings in Python. It is convenient to enclose large blocks of text in them. Apostrophes and regular quotes can be present inside the construction:

>>>Example_2=""Approximate set of words for a 'block string' in Python"""

Basic operations

Strings support the pressure of standard operations for sequences. These are concatenation, indexing, slice extraction, length calculation and repetition:

  • >>>Str_1="FB" Assign value
  • >>>Page_1
  • "FB"
  • >>>Str_2=Str_1 + "." + "ru"perform concatenation
  • >>>Page_2
  • "FB.ru"
  • >>>Str_3="O"3 + "PS!" repetition and concatenation
  • >>>P_3
  • "OOOPS!"
  • >>>len(Page_3) length calculation
  • 6

Because str are immutable types in Python, each operation creates a new string object.

Image
Image

Row indexing

Each element of a string can be accessed by its position or sequence number. The countdown does not start from the usual unit, but from zero. To work with indices, square brackets are used. Therefore, if you want to extract the second character, you need to pass the command “object name”[1] to the interpreter:

  • >>>Page_3[1]
  • "O"

When extracting a slice, the number to the left of the ":" operator means the left border inclusive. The number on the right indicates the element up to which the slice will be extracted. It is important to remember that the object specified to the right of the colon is not included in the slice:

  • >>>Page_3[3:5]
  • "PS"

String conversion

In Python, str() can be called as a built-in function. It takes any objects as an argument and returns their string representation. For example, if you need to concatenate, both sides of the "+" sign must have the same type of data. Otherwise, the interpreter will issue an error message:

  • >>>5+ "dogs" + "run"
  • Traceback (most recent call last): … TypeError
  • >>>str(5) + " dogs " + " run."
  • "5 dogs run."

A different repr() function is allowed instead of str(). It also performs the conversion, but returns the object as a line of code in an expanded form.

For deep processing of strings, there is a powerful set of methods specific to this data type. Formally, these are attributes attached to objects that refer to functions.

Syntactically, the construction of using string methods is as follows: "object.method(argument)".

  • >>>l="ggffkkllrr"
  • >>>l.replace("ff", "gg")
  • "ggggkkllrr"

The example used the.replace() method of replacing elements. This generic method accepts strings of any length as arguments and performs a global search followed by a replacement.

There are other methods and operators for working with strings, formatting and converting them. A complete list is in the official language guide.

Popular topic

Editor's choice

  • What is a proxy server and what is it for?
    What is a proxy server and what is it for?

    The word "proxy" has ever been heard by any of us, but not everyone knows what a proxy server is and what its purpose is

  • Google Chrome not working. What to do?
    Google Chrome not working. What to do?

    So why doesn't Chrome launch when we click on its desktop icon? Google Chrome not working for several reasons

  • Recovery console. Main benefits of using
    Recovery console. Main benefits of using

    Computers are quite technically complex devices. In this regard, many users from time to time encounter failures in the operation of the operating system and other programs. In most cases, you can fix problems in a matter of minutes, especially if the computer technician has the necessary skills. If a critical failure occurs, it is recommended to use the recovery console

  • What is the cluster size?
    What is the cluster size?

    The space of any storage medium (hard drive or flash drive) is not a whole piece, but a system of memory cells called clusters

  • What is defragmentation and why is it needed?
    What is defragmentation and why is it needed?

    Modern users are often spoiled by powerful computers and inexpensive components to such an extent that they do not even know the basic concepts. That is why they often find themselves in a situation where the car begins to shamelessly “slow down” and respond with extreme reluctance to any commands. As a rule, “evil viruses” that have entered the computer are blamed for everything, but sometimes the reality turns out to be much more prosaic