Python String Concatenation & Other Operation – Python Tutorial

1 year ago Lalit Bhagtani 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.
[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”100″]
[datacamp_sample_code]
# 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)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

String Repetition

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

[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”100″]
[datacamp_sample_code]
# create first string and assign it to variable first
first = ‘Hello ‘

# example of repetition
repetition = first * 3
print(repetition)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

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.

[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”100″]
[datacamp_sample_code]
# create first string and assign it to variable first
first = ‘Hello ‘

# example of contains
contains = ‘He’ in first
print(contains)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

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.

[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”100″]
[datacamp_sample_code]
# create first string and assign it to variable first
first = ‘Hello ‘

# example of not contains
not_contain = ‘He’ not in first
print(not_contain)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

String Length

We can use len() (Syntax: len(‘Your String’) ) function to get the length of a string. For example:
[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”50″]
[datacamp_pre_exercise_code]

[/datacamp_pre_exercise_code]
[datacamp_sample_code]
# 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)

[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[/datacamp_exercise]

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