Strings in Python | Learn Python Online
×

Strings in Python

2918

In Python, the string is one of the parts of data types used to store the sequence of characters including numbers, alphabets, and special characters enclosed by single quotes or double quotes.

Like another language, the definition of string is same that it is a sequence of characters. Our english language, there are 26 characters and our computers deal with binary numbers instead of characters. Characters are just symbols, that are internally manipulated and stored in our computer in the form of 0's and 1's combination where encoding and decoding take place for converting a character into number and numbers into characters respectively. Most common encodings are ASCII and Unicode. In python programming language, the string is known as a sequence of Unicode character that is written under single quotes. The Unicode string contains u as prefix and stored in 16 bit Unicode.

In this article, you can easily learn how to create strings, how to print strings, how to convert strings or in other words string conversion and string concatenation in python language by following the given examples

Creating strings:
Creating strings is as simple as you are assigning the values to the variable, only you have to enclose characters in quotes.

For example
variable3 = 'Hello Students!'
variable4 = "Welcome in Python Programming"

Printing string:
Strings can be displayed in the screen through a print statement.

Example
print"Welcome in python tutorial of codingtag"

Output
Welcome in python tutorial of codingtag

A simple program for creating and displaying strings
variable3 = 'Hello Students!'
variable4 = "Welcome in Python Programming"
print(variable3)
print(variable4)

Output
Hello Students!
Welcome in Python Programming


Updating existing string
Existing strings can be easily updated by reassigning a variable.

Example
variable1 = 'Hello student!'
print "modified String :- ", variable1[:6] + 'engineer'

Output
modified String :-  Hello engineer
replace() to replace string

Example
b = "Hello, World!"
print(b.replace("Hello", "Coding"))

Output
Coding, World!


Accessing characters by position:
Square brackets are used to access the specific elements of the string.

Example of getting the character at 5th position
var = "Welcome in codingtag!"
print(var[5])

Output
m

Example of a substring in python language
var = "Welcome in codingtag!"
print(var[3:8])

Output
come

Example to return the length of the string
var = "Welcome in codingtag!"
print(len(var))
#len() function

Output
21


Strings special operators
In python language +,*, [],[ : ], in, not in are known as strings special operators. The use of some special operators will be explained briefly with the help of examples in the following table.
In the given examples, two strings are used i.e. a1 = "coding" and a2 = "tag".

String special operators
Example
Output
Description
+

a1 = "coding"

a2 = "tag"

print(a1 + a2)

codingtag

Concatenation - this operator adds value on

either side of the operator

[]

a1= "coding"

print a1[1]

o
Output the character
[:]

a1= "coding"

print a1[1:4]
odi
Range slice
*

a1 = "coding"

a2 = "tag"

print(a1 + a2)
codingcodingcoding

Repetition of the strings along with -

concatenating multiple copies of the identical

string. i.e. It created three copies of a1 string.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments