String in Python

In this, string in Python tutorial we will learn how to create, access, format, update and delete strings.

What is a String

  • The String is a sequence of characters, also known as the text.
  • Each character of the string has a unique index number starting from zero (0).
  • Negative indexing is also possible in Python, the index of the last character is -1.
  • Example of the string is the name or address of a person.

How to Create a String in Python

String object can create in Python using both (single or double) quotes. Even triple quotes can be used for the multiline string in Python.

s1='Hello'  #text with single quote
s2="Hello"  #text with double quote
s3='''Hello'''  #text with triple quote
s4="""Example of
    Multiline String""" #text with multiline

#print all the strings
print(s1)
print(s2)
print(s3)
print(s4)

Access Characters and Slicing String

We can access the individual character of a string using an index, and we can access the range of a string using slicing. As we know the index of the first character is 0 and, the index of the last character is -1.

msg="Learning Python"

#print complete string
print(msg)

#print first character
print(msg[0])

#print third character
print(msg[2])

#print last character
print(msg[-1])

#print second last character
print(msg[-2])

#print first to fifth character
print(msg[0:5])

#print ninth to last (Python) character
print(msg[9:])

#loop through a string
for c in msg:
    print(c)

Python String Operations

We can perform many operations on the string in Python like delete, concatenate etc.

str1="Learning"
str2="Python"

#concatenate (+) two strings
print(str1 + str2)

#repeat string (*)
print(str2 * 3)

#compare strings, return true or false as result 
print(str1==str2)

#membership test (in and not in)
print('a' in str1)  #output: True
print('a' not in str1)  #output: False

#delete string
del str1
del str2
print(str1,str2)
#output: error

Python String format() method

The format() method of the string allows us substitution and value formating. The format() method formats the value and inserts them at the placeholders. The placeholder is defined using curly brackets, and we can use named, numbered and empty placeholders for the order of the values.

#default argument, follow sequence
s1="{} and {} is my favourite language".format("Python", "Java")
print(s1)

#positional or indexed argument
s1="{1} and {0} is my favourite language".format("Python", "Java")
print(s1)

#named argument (keyword argument)
s1="{f} and {s} is my favourite language".format(f="Python", s="Java")
print(s1)

Important String Functions in Python

In this article, we will learn about important useful string functions in Python.

  • len() – Return the length of the string.
  • upper() – Convert the string in uppercase.
  • lower() – Convert the string in lowercase.
  • split() – Split the string and returns as a list.
  • replace() – Accept two-argument, replace first with the second string.
  • find() – Find the index of a given substring in a string.
  • join() – Join first string with all characters of the second string.
  • reversed() – Reverse a given string.
msg="Learning Python"
print(msg)

#return length of string
print("Length :",len(msg))

#print string in uppercase
print("UPPER :",msg.upper())

#print string in lowercase
print("Lower :",msg.lower())

#split the string
print("Split :",msg.split())

#replace first string with second
print("Replace :",msg.replace('Learning','Learn'))

#return index of P.
print("Index :",msg.find("P"))

#join hyphen(-), after each character of a string
print('-'.join(msg))

#reverse the given string
print("Reversed :",''.join(reversed(msg)))

19
Created on By S. Shekhar

Python – Quiz 17

1 / 5

print(msg[:5]) will prints_____.

2 / 5

The placeholder is defined using curly brackets, and we can only use _____ placeholders for the order of the values.

3 / 5

Which of the following is used to reverse a string?

4 / 5

Which of the following returns a list?

5 / 5

Each character of the string has a unique index number starting from one.

Your score is

The average score is 74%

0%