0% found this document useful (0 votes)
6 views

String

Uploaded by

roymandesh3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

String

Uploaded by

roymandesh3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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:

 SYNTAX:- str = "Hi Python !"

print(type(str))
# Single quotes
string1 = 'Hello, World!'

# Double quotes
string2 = "Python is fun!"

# Triple quotes (for multi-line strings)


string3 = '''This is a
multi-line string.'''

 In Python, a string is a sequence of characters enclosed in quotes. You can use


either single quotes ('...') or double quotes ("...") to create a string. Python also
supports multi-line strings using triple quotes ('''...''' or """...""").
Python String Concatenation

 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.

 How to Concatenate Strings in Python


 Python String Concatenation is the technique
of combining two strings. Let’s look at different
ways to concatenate strings:
1. Using + operator
2. Using join() method
3. Using % operator
4. Using format() function
5. Using “,” (comma)
6. Using f-string ((Literal String Interpolation))
1. Using the + Operator:- It’s very easy to use the + operator for string concatenation. This operator can be
used to add multiple strings together. However, the arguments must be a string, if
used on integers it performs mathematical addition.

str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result)

# Output: Hello World

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)

# Output: Hello World

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)

# Output: Hello, Ram!


4. Using str.format(Method):- The str.format() is one of the string formatting methods in
Python, which allows multiple substitutions and value
formatting. It concatenates elements within a string
through positional formatting.
The curly braces {} are used to set the position of strings.

name = "Alice"
greeting = "Hello, {}!".format(name)
print(greeting)

# Output: Hello, Alice!


5. Using “,” (comma):- A comma “,” is a great alternative to string concatenation using “+”. when
you want to include a single whitespace. Use a comma when you want to
combine data types with single whitespace in between.

var1 = “python"
var2 = "for"
var3 = “python"

# using comma to combine data types


# with a single whitespace.
print(var1, var2, var3)
6. Using F-string for string concatenation :- Using F-string for string concatenation can only be done on
Python versions above 3.6

name = “RAM"
age = 25

# String concatenation using f-string


greeting = f"Hello, my name is {name} and I am {age} years
old."

print(greeting)

Hello, my name is RAM and I am 25 years old.

You might also like