Python Strings

What is string ? The data type used in programming to represent the text rather than numbers , are know as string.

Learn More about data types ( Numbers )

Strings in python : The text which is surrounded by either single quotation marks, or double quotation marks. i.e. “digitechbits”  is the same as ‘digitechbits’ . You can display a string literal with the print() function: To display a string in python we use print() function .

Example :

How to Assign String to a Variable in python ? Assigning a string to a variable is done with the variable name followed by an equal sign and the string: We can assign a string to a variable by writing a variable name , followed by an equals to sign (=) and the string (text). As,

Watch Video and learn with live example .

How to write Multiline Strings in python ? We can assign a multiline string to a variable by using three quotes Or three single quotes : Example

a = “””Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.”””
print(a)

Strings are Arrays in python ! 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.

What are Booleans , Read Now what are Operators

Example

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

a = "Hello, World!"
print(a[1])


Output : e

Loops using String :

As we know , in python strings are also a array . so we can loop through the characters in a string, using  for loop.

Example

Loop through the letters in the word “python”:

We will learn more about For Loops in our Python For Loops chapter.

How to get the length of a string in python ?

To get the length of a string in python , we have a function len() , we use this function to get the length of any string. Example :
a = “Hello, World!”
print(len(a))

Live Example

How to check if a certain phrase or character is present in a string in python or not ?

We use in keyword to check if a certain character or phrase is present in a string or not .
Example :
Check if “tech” is present in the following text:

txt = “digi tech bits tutorial on python string !”
print(“tech” in txt)

Live Example

What is the use of if keyword in string .

We can also use, if keyword , to check if a certain character or phrase is present in a string or not by creating a loop .
Example :
Print only if “tech” is present in the string stored in txt:

txt = “digi tech bits tutorial on python string!”
if “tech” in txt:
   print(“Yes, ‘tech’ is present in the string.”)

Live Example

What is the use of keyword not in.

We use not in keyword to check if a certain phrase or character is NOT present in a string. It will return a Boolean value . TRUE / FALSE
Example : Check if “java” is NOT present in the following text

txt = ” Digitechbits tutorial on python string !”
print(“java” not in txt)

We can also use an if statement for same as,

txt = “Digitechbits tutorial on python string”
if “java” not in txt:
  print(“Yes, ‘java’ is NOT present.”)

3 Replies to “Python Strings – Learn python”

Leave a Reply

Your email address will not be published.