String Append In Python
In this blog, you will know about different methods to append strings in Python with examples and proper explanations.
What are strings in python?
Strings are data types in python which are enclosed within a single or double quote.
Strings are immutable which means they cannot be modified. But we will see in this blog how can we append strings in python by different methods.
How can we append a string in Python?
We will not modify a string but rather add another string to it or in other words concatenate the strings. Concatenation of strings is very easy in python. We use the ” + ” symbol to add two strings.
a= "Hello" b= "World!" print(a+b)
The output of the above program is
Different methods to append strings in Python
1. using +=operator
2. using join() function
3. using user-defined append function
4. using f-strings in python 3.6
1. Using plus equal to (+=) operator:
Plus equal to (+=) is the addition assignment operator which adds the value of the right operand to a variable and assigns the result to the variable.
a += b means a= a+b
We can use this operator to append a string into another string
We can implement the above method in the following way:
str1 = "study" str2 = "experts" print("The first string: ",str1) print("The second string: ",str2) str1 += str2 #str1 = str1 + str2 print("The appended string: ",str1)
The output of the above program is:
2. Using join() function:
Join( ) is a function used to join all the elements of a list or tuple into a string separated by a specified string.
Syntax:
string.join(iterable)
here, the string is a specified separator, and iterable is any object where all the returned values are strings e.g. list or tuple, or dictionary.
We can implement the above method in the following way:
str1= "Python" str2= "is" str3= "fun" list= [str1, str2, str3] string= " ".join(list) print(string)
The output of the above program is:
3. Using user-defined string append function:
We will define a function append_string to append a string into another. The function append_string takes string str, a number num and a separator sep as input and the function will return the string separated by sep and appended num number of times.
In the below program we defined a function using the def keyword followed by the function name and all parameters provided in parenthesis. After defining we called the function and stored the value returned by it in a variable string.
We can implement the above method in the following way:
def append_string(str, num, sep) res= "" for i in range(num): res+= str + sep return res string= append_string("python",5,"-") print(string)
The output of the above program:
4. Using f-strings in Python
Python 3.6 provides us with f-strings, which we can use to append strings. This method is more readable and less prone to error.
We can implement the above method in the following way:
str1 = "Thank" str2 = "You" print("First string: ", str1) print("Second string: ", str2) print("Appended string: ", f"{str1}{str2}")
The output of the above program:
Also, read What is Naive Bayes and its works.
Pingback: Update key with new value : JavaScript - Study Experts