Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. String manipulation in python
  5. Basic operations on string in python

Basic operations on string in python

Basic operators

The two basic operators of strings are: ‘+’ and ‘*’.
We have used these operators as arithmetic operators for addition and multiplication respectively. But when used with strings, ‘+’ operator performs concatenation rather than addition and ‘*’ operator performs replication rather than multiplication.

Concatenation– Concatenation means that ‘+’ sign joins two strings and prints it.

e.g.,

s='python'
a='point'
print(s+a)
Output- pythonpoint

Replication– To use ‘*’ operator with strings, we need two type of operands- a string and a number. The string operand tells the string to be replicated and the number operand tells the number of times it is to be repeated.

e.g.,

s=" Hi"
a=3
print(s*a)
Output- Hi Hi Hi

Membership operators-

There are two membership operators for string:- in and not in.

in- Returns True if a character or a string exists in the given string, false otherwise.

e.g.,

s="pythonpoint"
print("n" in s)
Output- True

not in- Returns True if a character or a string does not exist in the given string, false otherwise.

e.g.,

s="pythonpoint"
print("z" not in s)
Output- True

This is all about basic operations on string in python.

Was this article helpful to you? Yes No

How can we help?