Python String Concatenation & Other Operation – Python Tutorial

3 weeks ago codedestine 0

Python has various types of string operators, which can be used to perform different operation on a given string like  python string concatenation, string repetition etc.

In this tutorial, we will learn about the following:

  1. String Concatenation
  2. String Repetition
  3. String Contains
  4. String Not Contains
  5. String Length

python string concatenation

Python String Concatenation

We can use addition ( + ) operator to concatenate ( join ) two strings together to create a new string.

# create first string and assign it to variable first first = 'Hello ' # create second string and assign it to variable second second = 'World' # example of concatenation concatenate = first + second print(concatenate)

String Repetition

We can use multiplication ( * ) operator to concatenate ( join ) same string multiple times to create a new string.

# create first string and assign it to variable first first = 'Hello ' # example of repetition repetition = first * 3 print(repetition)

String Contains

We can use in operator to check, if a given substring is present in the string or not. If present then return True otherwise return False.

# create first string and assign it to variable first first = 'Hello ' # example of contains contains = 'He' in first print(contains)

String Not Contains

We can use not in operator to check, if a given substring is present in the string or not. If present then return False otherwise return True.

# create first string and assign it to variable first first = 'Hello ' # example of not contains not_contain = 'He' not in first print(not_contain)

String Length

We can use len() (Syntax: len(‘Your String’) ) function to get the length of a string. For example:

# create one string and assign it to variable s s = 'Hello World' # call len() method to get length of the string s and assign it to variable length length = len(s) # print the value of variable length print(length)

References :-

  1. Strings Docs

That’s all for Python String Concatenation & Other Operation. If you liked it, please share your thoughts in comments section and share it with others too.

Previous