Python String

A string is a sequence of characters in Python. It is an immutable data type, meaning that once you have created a string, you cannot change it. Strings in Python are surrounded by either single quotation marks, or double quotation marks. For example, ‘myITSchools is the same as “myITSchools”. We can display a string literal with the print () function.

For example,



1) How to assign a string to a variable?

Assigning a string to a variable is done with the variable name followed by an equal sign and the string:

For example,



2) How to write multiline string in python?

You can assign a multiline string to a variable by using three quotes:

For example,

Data=\'\'\' Digital marketing is the component of marketing that,
uses the Internet and online-based,
digital technologies such as desktop computers, mobile phones,
and other digital media and platforms to promote products,
and services. \'\'\'

print (Data)

3) How to use string As Array?

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.

For example,

Get the character at position 3 (remember that the first character has the position 0):

name=\"welcome to myITSchool\"

4) How to use loop to print all characters of string?

As we know that strings are arrays or a sequence of characters, we can loop through the characters in a string, with a for loop.

Loop through the letters in the word \”myITSchool\”:

For example,

for x in \"myitschools\":

5) How to find string length in python?

To get the length of a string, we can use len () function.

For example,

The len() function returns the length of a string:


6) How to find the give string is text?

We can use keyword IN to find or check that certain phase or character is present in a string or not.

For example,

Check if \"myITSchool\" is present in the following text:

name=\"myitschools is a very informative website\"
print (\'myitschools\' in name)

7) How to use if statement to find text is the given sentence?

# Using if statement

name=\"myitschools is a very informative website\"
if \'myitschools\' Not in name:
    print (\"NO, \'myitschools\' is not present\");

8) How to use if statement to find text is not given in the sentence?

To check if a certain phrase or character is NOT present in a string, we can use the keyword Not In.

For example

name=\"myitschools is a very informative website\"
if \"myitschools\" not in txt:
  print (\"No, \'myitschools\' is NOT present.\")