Mohit Raj
Posted on November 4, 2019
Previous post in this series:
What is string ?
- Strings are the most popular types in Python.
- We can create them simply by enclosing characters in quotes.
- Python treats single quotes the same as double quotes.
- 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.
String concatenation
- combining two or more string in a single string.
- 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.
This concatenation is not possible with integers.
- String is only concatenation with string. if you try to concatenate one string with a number the it will show error. 2. Use of (*) in concatenation a. If you write str1="hello"*2 and print it then the output is hellohello 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.
Posted on November 4, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.