String objects with their operations in Python 3

In this tutorial, you will learn about the basics of strings in Python. We will learn about there operations and the difference between them. Lists and tuples are called compound data types whereas strings are immutable

String in Python

Strings are the sequence of characters in Python that are declared by using two quotes(either double or single).
We can also use single quotes.

s = 'Welcome to codespeedy'
a = "Hello world"

A string can also contain a sequence of spaces and digits or special characters.

i = "12345"
j = "#$@&%"

We can also bind a variable to a string. And each character in a string can be accessed by the indexing.

 

You can also read,

Characters of a string by its index in Python

exampl,

 string = "This is Me"

T-h-i-s- -i-s- -M-e
0-1-2-3-4-5-6-7-8-9

string[0]   : T
string[9]   : e
a[6] : s

Characters of a string by negative index in Python

You can also use negative index. But in this case you have to consider the index start point from the last character of your string

take the same example:

string = “This is Me”

T-h-i-s- -i-s- -M-e
-9/-8/-7/-6/-5/-4/-3/-2/-1

a[-1] : e
a[-6] : 
a[-8] : h

Slice a string in Python

We can use slicing in the string.

a = "Hello World"
print(a[2:6]) 
## o/p: 
print(a[::2])
## o/p:
Output:
"llo W"
"HloWrd"

Find the length of a string using len in Python

You can use len command to obtain the length of the string.

len("Welcome to codespeedy")
## length of the string : 21
x = "Python 3"
print(len(x))
## length of x: 8
Output:
21
8

String multiplication in Python – Replicate a string in Python

In Python, you can replicate the values of a string. You can simply multiply the string by the number, which results in the number of times you want to replicate, it in this case three. The result is a new string. Because it contains the copy of the original string.

3 * "CodeSpeedy"   -->   "CodeSpeedy CodeSpeedy CodeSpeedy"

This means that you cannot change the value of the string but you can create a new string.

String concatenation in Python – Add strings in Python

We can create a new string by applying concatenation to the string. This can be done by concatenating the original string with a new string using ‘+’ symbol.
As a result, we get a new string which can be stored in a new variable. This is shown below.

name = "Codespeedy"
statement = name + "is very useful"
print(statement)
Output:
Codespeedy is very useful

Change case of string in Python using Python inbuild methods

Strings are sequences, as you can apply methods to string ‘X’ and save the string ‘Y’ into the new variable.
Lets apply upper() and lower() methods.

string1 = "This is a new string."
string2 = string1.upper()
print(string2)


line1 = "Python Code"
line2 = line1.lower()
print(line2)
Output:
THIS IS A NEW STRING
python code

Please also refer to:

Mutable and Immutable objects in Python

Build a Number Guessing Game in Python

Leave a Reply

Your email address will not be published. Required fields are marked *