DEV Community 👩‍💻👨‍💻

Mohit Raj
Mohit Raj

Posted on

3.1 String Concatenation

Previous post in this series:

What is string ?

  1. Strings are the most popular types in Python.
  2. We can create them simply by enclosing characters in quotes.
  3. Python treats single quotes the same as double quotes.
  4. Creating strings is as simple as assigning a value to a variable. For example −

var1 = 'Hello World!'
var2 = "Python Programming"

{here we can say that var1 and var2 are two string variable.for proofing this we use type() function.}

=>> you can convert a number into a string by putting it into the single or double quotes.
Example:
x=2 (here 2 is treated as a number)
x='2' or x="2" (here 2 is string. known this by using type function)

Type() function

type() method returns class type of the argument(object) passed as parameter. (more details in upcoming posts)
Synatx : type(paramenter)

Here we have two variable var1 and var2 so we use type function to know its data types we write
type(var1)
type(var2) , and then print it.
Alt Text

String concatenation

  1. combining two or more string in a single string.
  2. In order to merge two strings into a single object/string, you may use the “+” operator. When writing code, that would look like this:

str1="hello"
str2="World"
str3=str1+str2
, output is helloWorld.
Alt Text This concatenation is not possible with integers.

  1. String is only concatenation with string. if you try to concatenate one string with a number the it will show error. Alt Text 2. Use of (*) in concatenation a. If you write str1="hello"*2 and print it then the output is hellohello Alt Text Its meaning is similar as for the numbers when you write 4*2 it means you add 4 two times. similarly here when we write "hello"*2 it add "hello" two time ("hello"+"hello")=hellohello. Alt Text

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.