Python String

The Python String is nothing but a sequence of characters. In this section, we discuss how to create a String, Accessing String items (individual characters), iterating the strings. Also, show you slicing String in Python programming language.

Create a Python String

There are multiple ways to create a string in Python. This gives you an idea to create a string.

# Creating a String

# Creating String using Single Quote
Str1 = 'This is a String'
print(Str1)

# Creating String using Double Quote
Str2 = "This is another String with Double Quotes"
print(Str2)

# Creating String using Multiple Single Quotes
Str3 = '''This also Work in Python'''
print(Str3)

# Creating String using Triple Quotes
# This is Very Useful to create Multiline Text
Str4 = """Learn Python Programming
          at Tutorial Gateway"""
print(Str4)
This is a String
This is another String with Double Quotes
This also Work in Python
Learn Python Programming
          at Tutorial Gateway

Access Python String items

We can access the elements in a Python String using indexes. Using an index, we can access each string item separately. Index value starts at 0 and ends at n-1, where n is the String length. For example, if a string length is 5, the index starts at 0 and ends with 4. To access 1st value, use String_name[0] and to access the 5th value, then use String_name[4].

x = 'Tutorial Gateway'

# Positive Indexing
print(x[0])
print(x[4])
print(x[9])
print('=======\n')

# Negative Indexing
print(x[-3])
print(x[-7])
print(x[-16])
print('=======\n')

If you are using the Negative index numbers, the Python starts looking for items from right to left (Here, -1 is the Last Item, -2 is Last but one, etc).

T
r
G
=======

w
G
T
=======

Iterate String

The For Loop is the most common way to traverse the items in a Python string. This code helps us to iterate the string str, and prints each character present in the str.

TIP: Please refer to For Loop and String Functions.

Str = "Python"

for word in Str:
    print("Letters are: ", word)

The above-specified code works nicely to print the characters inside the string. However, to change the individual item, we need the Python index position also. To fix this, we have to use the range function along with for loop

for word in range(len(Str)):
    print("Letters at index {0} is = {1}".format(word, Str[word]))
Iterating Python String

String Concatenation

Joining or combining more than one string is called as the Python string concatenation. There are multiple ways to concat strings

  • We can use + operator to join more than one string
  • By placing more than one string literal, joins them automatically.
  • Placing more than one string inside the one and closed parentheses ()
  • * operator is repeating the string for a given number of times. Here it is three times.

In this example, we show you how to use Arithmetic Operators on Python String to perform arithmetic Operations.

X = 'Tutorial '
Y = 'Gateway'

# Using + Operator
Concat = X + Y 
Concat1 = 'Python ' + X

# Placing two string Literals Together
Concat2 = 'Tutorial ' 'Gateway'

#Using Parentheses
Concat3 = ('Tutorial ' 'Gateway') 

print(Concat)
print(Concat1)
print(Concat2)
print(Concat3)

# Using * Operator
Z = X * 3
print(Z)
Tutorial Gateway
Python Tutorial 
Tutorial Gateway
Tutorial Gateway
Tutorial Tutorial Tutorial 

Python String Slice

In python String Slice, the first integer value is the index position where the slicing start and the second is where the slicing end, but it does not include the content at this index position. For instance, if we define string_item[1:4], then string slicing starts at index position 1 and ends at index position 3 (not 4).

x = 'Tutorial Gateway'

# Slicing the String using two indexes
a = x[2:13] 
print("Both Indexes = ", a)

# Slicing the String using Second indexes
b = x[:8] 
print("No First Index = ", b)

# Slicing the String using First indexes
c = x[4:] 
print("No Second Index = ", c)

# Slicing the String without using two indexes
d = x[:] 
print("No Indexes = ", d)

# Slicing the String using Negative indexes
e = x[-3:] 
print("Negative First Index = ", e)

# Slicing the String using Negative indexes
f = x[:-2] 
print("Negative Second Index = ", f)
Both Indexes =  torial Gate
No First Index =  Tutorial
No Second Index =  rial Gateway
No Indexes =  Tutorial Gateway
Negative First Index =  way
Negative Second Index =  Tutorial Gatew

String slice analysis

  • If you omit the first index, the string slicing starts from the beginning.
  • If you omit the second index, String Slicing starts from the first index and continues to the last.
  • And, If you are using the Negative numbers as an index, the string slicing starts from right to left.