Sitemap
Naga Durga Bhavani. Kokkira

Naga Durga Bhavani. Kokkira

Strings

4 min readOct 17, 2023

Strings are characters or a sequence of characters, enclosed in quotes(‘single quote’, “double quotes” or “‘triple quotes”’)

“‘Triple quotes’” are used as multiline strings, multiline comments and as doc strings

Creating a sample string

Press enter or click to view image in full size

Escape Characters :

→ An escape sequence contains a backslash (\) symbol followed by one of the escape sequence characters or an octal or hexadecimal number. A hexadecimal escape sequence contains an x followed by one or more hexadecimal digits (0–9, A-F, a-f). An octal escape sequence uses up to three octal digits (0–7).

  • \n — ->Is moving to the next line
  • \t — -> Tab space
  • \r — ->It moves the cursur to start of the line
Press enter or click to view image in full size

Raw String and format string :

Raw string :

A raw string, also known as a raw literal or raw text literal, is a way to represent strings in programming languages, where escape sequences are not processed or interpreted. In many programming languages, escape sequences are used to represent special characters, such as newline characters (\n) or tabs (\t), within a string. However, in a raw string, these escape sequences are treated as literal characters, and their backslashes are not used for escaping.

The exact syntax for creating raw strings may vary from one programming language to another, but in many languages, an “r” (or some other character) is placed in front of the string literal to indicate that it should be treated as a raw string. For example, in Python, you can create a raw string like this:

Press enter or click to view image in full size

In the above Python example, the string raw _string is a raw string, and the backslash before the "n" is treated as a literal backslash, and not as an escape sequence for a newline character.

Raw strings are often used when dealing with regular expressions, file paths, or any other context where backslashes or escape sequences could be problematic. They help avoid unintended escaping and make it easier to work with literal representations of text.

Indexing :

In Python, indexing is the process of accessing individual elements of a data structure, such as a list, tuple, string, or other sequence-like objects. Indexing allows you to retrieve specific items from the collection by specifying their position within the sequence. Python uses zero-based indexing, which means the first element is at index 0, the second element is at index 1, and so on.

Get Naga Durga Bhavani. Kokkira’s stories in your inbox

Join Medium for free to get updates from this writer.

Here’s how indexing works for various data structures:

  • Indexing in Strings:

Strings are sequences of characters, and you can access individual characters by their index.

Press enter or click to view image in full size
Press enter or click to view image in full size
  • Negative Indexing:

You can also use negative indices to access elements from the end of a sequence. -1 refers to the last element, -2 refers to the second-to-last element, and so on.

Press enter or click to view image in full size
Press enter or click to view image in full size
  • Slicing
  • Slicing is a more versatile way to extract a portion of a sequence. You specify a range of indices to create a new sequence containing elements from the original.
  • To slicing we need 3 values [Starting index : ending index : step size]
Press enter or click to view image in full size
Press enter or click to view image in full size

Keep in mind that attempting to access an index that doesn’t exist will result in an “Index Error.” It’s important to ensure that the index you use is within the valid range for the data structure.

Concatenation :

  • Concatenation in Python refers to the process of combining two or more strings, lists, or other sequences to create a new sequence. The + operator is commonly used for concatenating strings, while the extend() method or the + operator can be used for concatenating lists.

— → Here are examples of concatenation in Python:

  • Concatenating Strings:
  • You can use the + operator to concatenate two or more strings together:
Press enter or click to view image in full size

In Python, different data types have different rules for concatenation, so make sure to handle data type conversions as needed.

--

--