Python – String

After reading this Python String topic, you will understand how to write and manipulate the string, and you will understand how to + operator for string use in Python.

A group of characters is known as string. Characters that can be used in string as lowercase letters (a – z), uppercase letters (A – Z), digits (0 – 9) and other characters as (#, %, &, etc.).

How to write string in python?

A string writes inside a pair of single quotation mark or a string can also write inside a pair of a double quotation mark.

Example:

Program (1): To demonstrate how to write and display string in Python.

# string write inside a pair of single quotation mark
x='programming'
print(x)
# string write inside a pair of double quotation mark
y="programming"
print(y)

Output(1)

programming
programming

Explanation: for Program(1) and Output(1)

  • statement x=’programming’ gives output as programming.
  • statement y=”programming” gives output as programming.
  • So, the string writes inside a pair of single or double quotation mark gives the same output.

Accessing Character from String

Example:

Program (1): To demonstrate how to access character from string in Python.

# string write inside a pair of single quotation mark
x='programming'
print(x[0])
print(x[1])

Output(1)

p
r

Explanation: for Program(1) and Output(1)

  • statement print(x[0]) gives output p describe the first character of string (i.e. programming) is associated with index number 0.
  • statement print(x[1]) gives output r describe the second character of string (i.e. programming) is associated with index number 1.

Accessing Multiple Character from String

Example:

Program (1): To demonstrate how to access multiple characters from the string in Python.

# string write inside a pair of single quotation mark
x='programming'
print(x[1:4])

Output(1)

rog

Explanation: for Program(1) and Output(1)

  • statement print(x[1:4]) gives output rog describe accessing characters of the string associated with index numbers 1 to 3.

+ operator for Strings

The + operator use for two or more strings concatenation.

Example:

Program (1): To demonstrate how to concatenate any two string in Python.

y='python '
x='programming'
print(y+x)

Output(1)

python programming

Explanation: for Program(1) and Output(1)

  • variable y stores string as python.
  • variable x stores string as programming.
  • statement print(y+x) represents a string of variable y concatenate with a string of variable x and hence the output is python programming.

String handling built-in Methods

Some of the commonly used string handling methods like len(), lower() and upper().

len(): This method calculates and gives the length of the string.

Example:

Program (1): To demonstrate how to use len() function in Python.

x='programming'
print(len(x))

Output(1)

11

Explanation: for Program(1) and Output(1)

  • The length of string ‘programming’ is 11.

lower() : This method convert complete string into lowercase string.

Example:

Program (1): To demonstrate how to use lower() function in Python.

x='PYTHON'
print(x.lower())

Output(1)

python

Explanation: for Program(1) and Output(1)

  • The variable x stores the string ‘PYTHON’.
  • statement print(x.lower()), print and convert string ‘PYTHON’ into the lowercase string as python.

upper(): This method converts the complete string into the uppercase string.

Program (1): To demonstrate how to use upper() function in Python.

x='python'
print(x.upper())

Output(1)

PYTHON

Explanation: for Program(1) and Output(1)

  • The variable x stores the string ‘python’.
  • statement print(x.upper()), print and convert string ‘python’ into the uppercase string as PYTHON.

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *