Python Strings

Python has a built-in string class named "str" with many useful features.
String literals can be enclosed by either single or double, although single quotes are more commonly used.
Backslash escapes work the usual way within both single and double quoted literals -- e.g. \n \' \".
A double quoted string literal can contain single quotes without any fuss (e.g. "I wouldn't be a painter") and likewise single quoted string can contain double quotes.
A string literal can span multiple lines, use triple quotes to start and end them.
You can use single quotes too, but there must be a backslash \ at the end of each line to escape the newline.
Strings in python are surrounded by either single quotation marks, or double quotation marks.
'hello' is the same as "hello".
You can display a string literal with the print() function:
Example:

print("Hello RGUKT")
print('Hello RGUKT')

Assign Strings to a Variable

Assigning a string to a variable is done with the variable name followed by an equal sign and the string.
To assign it to a variable, we can use the variable name and “=” operator.
Normally single and double quotes are used to assign a string with a single line of character but triple quotes are used to assign a string with multi-lines of character.
This is how to declare and assign a variable to a string in Python.
Example:

a = "hello rguktians"
print(a)

Multiline Strings

A multiline string in Python begins and ends with either three single quotes or three double quotes.
Any quotes, tabs, or newlines in between the “triple quotes” are considered part of the string.
Python's indentation rules for blocks do not apply to lines inside a multiline string.
You can assign a multiline string to a variable by using three quotes.
Example:
double quotes.

a = """You can assign a multiline string,
to a variable by using three quotes,
A multiline string in Python begins and,
ends with either three single quotes or three double quotes."""
print(a)
single quotes.
a = '''You can assign a multiline string,
to a variable by using three quotes,
A multiline string in Python begins and,
ends with either three single quotes or three double quotes.'''
print(a)         
Note: in the result, the line breaks are inserted at the same position as in the code.

Strings are Arrays

Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters.
However, Python does not have a character data type, a single character is simply a string with a length of 1.
Square brackets can be used to access elements of the string.
Example:

x = "Hello,Rguktians!"
print(x[1])

Looping Through String

You can traverse a string as a substring by using the Python slice operator ([]).
It cuts off a substring from the original string and thus allows to iterate over it partially.
To use this method, provide the starting and ending indices along with a step value and then traverse the string.
One way to iterate over a string is to use for i in range(len(str)).
In this loop, the variable i receives the index so that each character can be accessed using str[i].
Example:

for x in "RGUKT":
    print(x)

String Length

The length or size of a string means the total number of characters present in it.
For Example: The string “rguktweb python” has 15 characters (including spaces also).
The length of a string in Python refers to the number of characters in the string or the size of the string.
For example: The length of the string “Interview” is 9 because it has nine characters.
Example:

a = "hello RGUKTIANS"
print(len(a))

Checking String

In python programming we can check whether strings are equal or not using the “==” or by using the “_eq_” function.
it returns a Boolean (either True or False ).
To check if a string contains a substring in Python using the in operator,
we simply invoke it on the superstring.
You can also use the type built-in function to see if the type of your variable is str.
Example:

a = "check whether strings are equal or not"
print("whether" in a)

Check if Not

The 'not' is a Logical operator in Python that will return True if the expression is False.
The 'not' operator is used in the if statements.
If x is True, then not will evaluate as false, otherwise, True.
“not in” operator − This operator is used to check whether an element is not present in the passed list or not.
Returns true if the element is not present in the list otherwise returns false.
Example:

a = "check whether strings are equal or not"
print("RGUKT" not in a)
Another example using if Example
a = "check whether strings are equal or not"
if "RGUKT" not in a:
    print("NO,'RGUKT'is not present")