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

Python String 1

Uploaded by

payal.raskar2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python String 1

Uploaded by

payal.raskar2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

TOPIC:-


PYTHON STRING
Define Python String
In Python, a string is a sequence of characters, typically
used to represent text. Strings are immutable, meaning
they cannot be changed after they are created. They can
be enclosed in either single quotes (''), double quotes ("")
or triple quotes (''' or """) depending on the requirement.
Python provides a wide range of methods and operations
to manipulate and work with strings efficiently.
CREATING A STRING
You can create a string in Python by simply enclosing text within
either single quotes (''), double quotes ("") or triple quotes (''' or """).
Here are some examples:single_quoted_string = 'Hello, world!'
double_quoted_string = "Hello, world!"
triple_quoted_string = '''Hello, world!'''
STRINGS ARE ARRAYS
In Python, strings can be indexed like arrays, meaning you can
access individual characters in a string using square brackets and
the index of the character. However, strings are not technically
arrays in Python. While they share some similarities with arrays in
other programming languages, such as C or Java, strings in Python
are immutable sequences of characters rather than mutable arrays.
For example:
```python
my_string = "Hello"
print(my_string[0]) # Prints 'H'
print(my_string[1]) # Prints 'e'
```
In this example, `my_string[0]` accesses the first character 'H' in the
string "Hello", and `my_string[1]` accesses the second character 'e'.
TYPES OF STRINGS
1).Single Quoted Strings: These strings are enclosed within
single quotation marks ('). For example: 'Hello'.Double Quoted
2).Strings: These strings are enclosed within double quotation
marks ("). For example: "Hello".
3).Triple Quoted Strings: These strings are enclosed within
triple quotation marks (''' or """). Triple quoted strings are often
used for multi-line strings or for strings that contain both single
and double quotes. For example:'''This is a multi-line
string.'''
"""This string contains 'single' and "double" quotes."""
EXAMPLE OF STRING
EXAMPLE
# Single quoted string
single_quoted_string = 'Hello, world!'
print(single_quoted_string)
# Double quoted string
double_quoted_string = "Hello, world!"
print(double_quoted_string)
# Triple quoted string for multi-line or strings with both single
and double quotes
triple_quoted_string = '''This is a multi-line
string with 'single' and "double" quotes.'''
print(triple_quoted_string)
OUTPUT OF EXAMPLE

Hello, world!
Hello, world!
This is a multi-line
string with 'single' and "double" quotes.

You might also like