In Python, when you prefix a string with the letter r or R such as r'...' and R'...', that string becomes a raw string. Unlike a regular string, a raw string treats the backslashes (\) as literal characters.
Raw strings are useful when you deal with strings that have many backslashes, for example directory paths on Windows.
Example:
s = "lang\tver\nPython\t3 print(s)"
Output:
Output:
lang ver
Python 3
However, raw strings treat the backslash (\) as a literal character. For example:
s = r"lang\tver\nPython\t3"print(s) #Output: lang\tver\nPython\t3
Convert a regular string into a raw string
To convert a regular string into a raw string, you use the built-in repr() function. For example:
s = '\n'raw_string = repr(s)print(raw_string) #Output: \n
In Python, you can compare two strings using comparison operators. The comparison operators return a Boolean value (True or False) depending on whether the comparison is true or false.
The comparison operators available for strings are:
== (equal to): returns True if the two strings are equal, and False otherwise.
!= (not equal to): returns True if the two strings are not equal, and False otherwise.
< (less than): returns True if the first string comes before the second string in alphabetical order, and False otherwise.
> (greater than): returns True if the first string comes after the second string in alphabetical order, and False otherwise.
<= (less than or equal to): returns True if the first string comes before or is equal to the second string in alphabetical order, and False otherwise.
>= (greater than or equal to): returns True if the first string comes after or is equal to the second string in alphabetical order, and False otherwise.
in: returns True if a substring is found within a string, and False otherwise.
not in: returns True if a substring is not found within a string, and False otherwise.
The islower() and isupper() functions return True if all characters in the string are lowercase or uppercase respectively, otherwise False. For example:
Slicing a string does not modify the original string - it returns a new string that contains only the specified portion of the original string, because strings are immutable
You can slice a string in Python using the syntax string[start:end:step], where:
start is the index of the first character you want to include in the slice
end is the index of the first character you want to exclude from the slice
step is the number of characters to skip between each character included in the slice
Here are some examples:
string = "Hello, World!"# slice the entire stringprint(string[:]) # Output: Hello, World!# slice from index 0 to index 5 (excluding 5)print(string[0:5]) # Output: Hello# slice from index 7 to the endprint(string[7:]) # Output: World!# slice from index 2 to index 10, skipping every other characterprint(string[2:10:2]) # Output: lo,W# slice from the beginning to the end, skipping every other characterprint(string[::2]) # Output: Hlo ol!# slice from the end of the string to the beginningprint(string[::-1]) # Output: !dlroW ,olleH# slice from the end of the string to index 2 (excluding 2), skipping every other characterprint(string[-1:1:-2]) # Output: !lo le# slice from index -5 (5th character from the end) to index -1 (excluding -1), skipping every other characterprint(string[-5:-1:2]) # Output: Wo# slice from index -10 (10th character from the end) to index -7 (excluding -7)print(string[-10:-7]) # Output: orl# slice from index -3 (3rd character from the end) to the end of the stringprint(string[-3:]) # Output: ld!
Note:
That you can mix positive and negative indices in a single slice, and you can omit any of the three parameters (start, end, and step) as needed.
In Python, a string is a sequence of characters, and you can access individual characters in the string by their position or index within the sequence.
The index of a string starts at 0, which means that the first character in the string has an index of 0
The second character has an index of 1, and so on.
+---+---+---+---+---+ | h | e | l | l | o | +---+---+---+---+---+ | 0 | 1 | 2 | 3 | 4 | +---+---+---+---+---+ |-5 |-4 |-3 |-2 |-1 | +---+---+---+---+---+
To access a specific character in a string, you can use its index within square brackets [], like this:
In this example, we access each character in the string string by its index within square brackets [].
Note:
The index must be an integer, and it must be within the range of valid indices for the string (from 0 to len(string) - 1). Otherwise, a IndexError will be raised.