String
String
Strings in python are surrounded by either single quotation marks, or double quotation marks.
Python string is the collection of the characters surrounded by single quotes, double quotes, or triple
quotes. The computer does not understand the characters; internally, it stores manipulated character as the
combination of the 0's and 1's.
Each character is encoded in the ASCII or Unicode character. So we can say that Python strings are also
called the collection of Unicode characters.
In Python, strings can be created by enclosing the character or the sequence of characters in the quotes.
Python allows us to use single quotes, double quotes, or triple quotes to create the string. Syntax:
print(type(str))
# Single quotes
string1 = 'Hello, World!'
# Double quotes
string2 = "Python is fun!"
String concatenation is the process of joining two or more strings together and forming one single string. You
can use the + operator, or use a built-in function like str.join() to concatenate two strings.
Strings are arrays of bytes representing Unicode characters. However, Python does not have a character data
type, a single character is simply a string with a length of “1”.
Square brackets [ ] can be used to access elements of the string. In this tutorial, we will learn how to
concatenate strings in Python with examples and Python programs.
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result)
2. Using the join() Method :- The join() method is a string method and returns a string in which the elements of the
sequence have been joined by a string separator.
It accepts only the list as its argument and list size can be anything. Here, we will
see about Python concatenate string done by using join()
words = ["Hello", "World"]
result = " ".join(words)
print(result)
3.Using String Formatting (% Operator):- We can use the % operator for string formatting, it can also
be used for string concatenation. It’s useful when we want to
concatenate strings and perform simple formatting.
name = “Ram"
greeting = "Hello, %s!" % name
print(greeting)
name = "Alice"
greeting = "Hello, {}!".format(name)
print(greeting)
var1 = “python"
var2 = "for"
var3 = “python"
name = “RAM"
age = 25
print(greeting)