Interesting facts about strings in Python | Set 2 (Slicing)
Last Updated :
04 Aug, 2022
Creating a String
Strings in Python can be created using single quotes or double quotes or even triple quotes.
# Python Program for
# Creation of String
# Creating a String
# with single Quotes
String1 = 'Welcome to the Geeks World'
print("String with the use of Single Quotes: ")
print(String1)
# Creating a String
# with double Quotes
String1 = "I'm a Geek"
print("\nString with the use of Double Quotes: ")
print(String1)
# Creating a String
# with triple Quotes
String1 = '''I'm a Geek and I live in a world of "Geeks"'''
print("\nString with the use of Triple Quotes: ")
print(String1)
# Creating String with triple
# Quotes allows multiple lines
String1 = '''Geeks
For
Life'''
print("\nCreating a multiline String: ")
print(String1)
Output:
String with the use of Single Quotes:
Welcome to the Geeks World
String with the use of Double Quotes:
I'm a Geek
String with the use of Triple Quotes:
I'm a Geek and I live in a world of "Geeks"
Creating a multiline String:
Geeks
For
Life
Accessing characters in Python
In Python, individual characters of a String can be accessed by using the method of Indexing. Indexing allows negative address references to access characters from the back of the String, e.g. -1 refers to the last character, -2 refers to the second last character, and so on.
While accessing an index out of the range will cause an IndexError. Only Integers are allowed to be passed as an index, float or other types that will cause a TypeError.
# Python Program to Access
# characters of String
String1 = "GeeksForGeeks"
print("Initial String: ")
print(String1)
# Printing First character
print("\nFirst character of String is: ")
print(String1[0])
# Printing Last character
print("\nLast character of String is: ")
print(String1[-1])
Output:
Initial String:
GeeksForGeeks
First character of String is:
G
Last character of String is:
s
String Slicing
Like other programming languages, it's possible to access individual characters of a string by using array-like indexing syntax. In this we can access each and every element of string through their index number and the indexing starts from 0. Python does index out of bound checking.
So, we can obtain the required character using syntax, string_name[index_position]:
- The positive index_position denotes the element from the starting(0) and the negative index shows the index from the end(-1).
Example:
Python3
# A python program to illustrate slicing in strings
x = "Geeks at work"
# Prints 3rd character beginning from 0
print (x[2])
# Prints 7th character
print (x[6])
# Prints 3rd character from the rear beginning from -1
print (x[-3])
# Length of string is 10 so it is out of bound
print (x[15])
Output:
Traceback (most recent call last):
File "8a33ebbf716678c881331d75e0b85fe6.py", line 15, in <module>
print x[15]
IndexError: string index out of range
e
a
o
Slicing
To extract substring from the whole string then we use the syntax like
string_name[beginning: end : step]
- beginning represents the starting index of string
- end denotes the end index of string which is not inclusiveÂ
- steps denotes the distance between the two words.
Note: We can also slice the string using beginning and only and
steps are optional.
Example:
Python3
# A python program to illustrate
# print substrings of a string
x = "Welcome to GeeksforGeeks"
# Prints substring from 2nd to 5th character
print (x[2:5])
# Prints substring stepping up 2nd character
# from 4th to 10th character
print (x[4:10:2])
# Prints 3rd character from rear from 3 to 5
print (x[-5:-3])
Output:
lco
oet
Ge
How to print single quote or double quote on screen?
We can do that in the following two ways:
- First one is to use escape character to display the additional quote.
- The second way is by using mix quote, i.e., when we want to print single quote then using double quotes as delimiters and vice-versa.
Example-
Python3
print("Hi Mr Geek.")
# use of escape sequence
print("He said, \"Welcome to GeeksforGeeks\"")
print('Hey so happy to be here')
# use of mix quotes
print ('Getting Geeky, "Loving it"')
Output:
Hi Mr Geek.
He said, "Welcome to GeeksforGeeks"
Hey so happy to be here
Getting Geeky, "Loving it"
How to print escape character instead?
If there is a requirement of printing the escape character(\) instead,then if user mention it in a string interpreter will think of it as escape character and will not print it.In order to print the escape character user have to use escape character before
'\' as shown in the example.
Python3
# Print Escape character
print (" \\ is back slash ")
Output:
\ is back slash
Similar Reads
How to Index and Slice Strings in Python? In Python, indexing and slicing are techniques used to access specific characters or parts of a string. Indexing means referring to an element of an iterable by its position whereas slicing is a feature that enables accessing parts of the sequence.Table of ContentIndexing Strings in PythonAccessing
2 min read
String Slicing in Python String slicing in Python is a way to get specific parts of a string by using start, end and step values. Itâs especially useful for text manipulation and data parsing.Letâs take a quick example of string slicing:Pythons = "Hello, Python!" print(s[0:5])OutputHello Explanation: In this example, we use
4 min read
Python | Filter String with substring at specific position Sometimes, while working with Python string lists, we can have a problem in which we need to extract only those lists that have a specific substring at a specific position. This kind of problem can come in data processing and web development domains. Let us discuss certain ways in which this task ca
7 min read
Split and Parse a string in Python In this article, we'll look at different ways to split and parse strings in Python. Let's understand this with the help of a basic example:Pythons = "geeks,for,geeks" # Split the string by commas res = s.split(',') # Parse the list and print each element for item in res: print(item)Outputgeeks for g
2 min read
Python | Set 3 (Strings, Lists, Tuples, Iterations) In the previous article, we read about the basics of Python. Now, we continue with some more python concepts. Strings in Python: A string is a sequence of characters that can be a combination of letters, numbers, and special characters. It can be declared in python by using single quotes, double quo
3 min read