String in Python with Single, Multi-Line, Concatenate in Python

First of all you have to understand about String in Python. Basically a String is a collection of characters which is used to define a word or sentence or paragraph with single or multiple lines. And here we also describe some basic Format of String or Integer Concatenation in Python.

Use String in Python

In Python, when we define a string, it will be define either Single (‘ ’) Quote or Double Quote ( “ ”).
For simple example you can use print() function for display simple word or sentence as form of string as given bellow:

print( "SKOTechLearn" )
print( 'SKOTechLearn' )
Output:
>>>
SKOTechLearn
SKOTechLearn
>>>


So, let’s see the types and use of String with following way:

  1. Single Line String using Variable 
  2. Use String to Passing value in Function 
  3. Multi-Line Sting using Variable 
  4. Concatenate Strings in Python 
  5. Concatenate integer or number with String


(1). Single Line String in Python using Variable :

Suppose you want to store your word or sentence and show during process then we simply assign a Variable.

mystrng = "Hello SKOTechLearn"
print(mystrng)
Output:
>>>
Hello SKOTechLearn
>>>


(2). Use String to Passing value in Function in Python:

This is very important point which is used in every programming process. Here you can use string for passing in other function or references.

You can simply cover following points:

➤ Python pass function as argument with parameters
➤ How to pass string as parameter in python function?
➤ Python pass function name as argument
➤ Python call function from string with arguments

You have to simply learn following process:

Myfstr = "skotechlearn"
MyPFunc( Myfstr )

def MyPFunc ( Myfstr ):
    # upper() method is use to show Characters in Capital Letters
    skstr = Myfstr.upper()
    print (skstr)
Output:
>>>
SKOTECHLEARN
>>>

You can simply use this process with many ways according to your application requirement process.


(3). Multi-Line Sting in Python using Variable :

As you can understand how to store or show string in python, but when you want to store multiline sentence or paragraph on your application, what is the code process we apply?

You have to simply write word or sentence in start and end with Triple Quotes (""" """) or You can simply use “\n” between you sentence with following way:

Multi-line String using Triple Quotes (""" """) :

MyMultilineStr = """This is Triple Quote multiline Sentence
Using Python Code
With Simple output"""
print (MyMultilineStr)
Output:
>>>
This is Triple Quote multiline Sentence
Using Python Code
With Simple output
>>>


Multi-line using “\n” :

MyNextLineStr = "This is multiline Sentence \n Python Code \n With Next Line Process "
print (MyNextLineStr)
Output:
>>>
This is multiline Sentence
Python Code
With Next Line Process
>>>


(4). Concatenate Strings in Python :

There are 4 ways for concatenating string in Python.

First Process:


You can simply use “+” Sign for concatenate word or sentence.

FstStr = "SKO"
SecStr = "Tech"
ThirdStr = "Learn"

FinalStr = FstStr  + " " + SecStr + " " + ThirdStr
print (FinalStr)
Output:
>>>
SKO Tech Learn
>>>


Second Process:


You can simply use “{}{}{}”.format(StrVar1, StrVar2, StrVar3) function.

Tips1: "{}{}{}".format(StrVar1, StrVar2, StrVar3): if you don’t need space between string.
Tips2: "{} {} {}".format(StrVar1, StrVar2, StrVar3) : if you need to add space between string.

Note: Input Number of “{}” Curly Brace according to the number of string variable.

PStr1= "SKO"
PStr2 = "Tech"
PStr3 = "Learn"
PStr4 = "Tips"

#concatenate String without space
print("{}{}{}{}".format(PStr1, PStr2, PStr3, PStr4)) 

# concatenate String with space
print("{} {} {} {}".format(PStr1, PStr2, PStr3, PStr4))
Output:
>>>
SKOTechLearnTips
SKO Tech Learn Tips
>>>


Third Process:


In this process you can use "% s % s % s" % (StrVr1, StrVr2, StrVr3). Here you can add number of “% s” according to number of string value or variable.

"% s % s % s" : if you want to add space during concatenating variable. This will show sentence with space.
"% s% s% s" : This will show Concatenate variable without space.

For Example:
myVr1 = "This"
myVr2 = "is"
myVr3 = "Concatenate"
myVr4 = "Code"
#Sentance without space
print("%s%s%s%s" % (myVr1, myVr2, myVr3, myVr4)) 

# Add space between word or sentence
print("%s %s %s %s" % (myVr1, myVr2, myVr3, myVr4))
Output:
>>>
ThisisConcatenateCode
This is Concatenate Code
>>>


Forth Process:


Use .join for concatenate in python. For Simply you have to follow following syntax:

Concatenate Without Space: "".join([SkVr1, SkVr2, SkVr3])
Concatenate With Space: "".join([SkVr1, SkVr2, SkVr3])

For Example:
SkVr1 = "Learn"
SkVr2 = "Concatenate"
SkVr3 = "process"
# Concatenate process without space
print("".join([SkVr1, SkVr2, SkVr3])) 
# Concatenate process with space 
print(" ".join([SkVr1, SkVr2, SkVr3]))
Output:
>>>
LearnConcatenateprocess
Learn Concatenate process
>>>


(5). Concatenate integer or number in Python with String:

When you will try to concatenate integer in Python with alphabet or sentence, you will get the following error with normal process like bellow:
print(1 + "This is Alphabet")
>>>
Traceback (most recent call last):
 print(1+"This is Alphabet")
TypeError: unsupported operand type(s) for +: 'int' and 'str
>>> 

As you can see the above error will display. So, we try following way to solve this problem. There are four process for Concatenating Integer with String:

Process 1:
 Simply input number inside Double Quote(“ ”):

print("2"+ ". This is Alphabet")
Output:
>>>
2. This is Alphabet
>>>


Process 2:
 Using str() for converting Integer :

mynum = 2
combstr = "This is Alphabet"
print(str(mynum) + "=  " + combstr)
Output:
>>>
2= This is Alphabet
>>>


Process 3:
 Using .format method for concatenate of integer :

mynum = 2
combstr = "This is Alphabet"
print "Point No. {} is - {} for concatenate with Integer".format(mynum, combstr)
Output:
>>>
Point No. 2 is - This is Alphabet for concatenate with Integer
>>>


Process 4:
 Using “% s” for Concatenate integer :

mynum = 2
combstr = "This is Alphabet"
print("No. % s % s" % (mynum, combstr))
Output:
>>>
No. 2 This is Alphabet
>>>


So Friends, these are the process for Using String in Python, easily show Multi-line String in python, Passing String in function in Python, Concatenate String in Python, Concatenate Integer in Python.

0 comments: