12/03/2022

A string is a sequence of characters. A character is simply a symbol. For example, the English language has 26 characters.
Computers do not deal with characters, they deal with numbers (binary). Even though you
may see characters on your screen, internally so, it is store and manipulate as a combination
of 0’s and 1’s.

Then, This conversion of a character to a number is call encoding, and the reverse process is
decoding. Moreover
ASCII and Unicode are some of the popular encoding used.
So, In Python, the string is a sequence of Unicode characters. After that Unicode was introduce to include every character in all languages and bring uniformity in encoding.

How to access characters in a String in python?

  1. So, We can access individual characters using indexing and a range of characters using
    slicing.
  2. Moreover, Index starts from 0.
  3. Then, After Trying to access a character out of the index range will raise an Index Error.
  4. The index must be an integer.
  5. Then, We can’t use float or other types, this will result in TypeError.
  6. So, Python allows negative indexing for its sequences.
  7. The index of -1 refers to the last item, -2 to the second last item, and so on.
  8. Then, We can access a range of items in a string by using the slicing operator (colon).
  9. At last, Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods.

Go for online python compiler and you can compile it

Python types of string Operations:

Concatenation of Two or More Strings:

Joining two or more strings into a single one is called concatenation. The + operator does this in Python. Simply writing two-string literals together also concatenates them.
The * operator can be used to repeat the string a given number of times.

str1 = 'Hello'
str2 ='World!'
using +
print('str1 + str2 = ', str1 + str2)
using *
print('str1 * 3 =', str1 * 3)
str1 + str2 =HelloWorld!
str1 * 3 = HelloHelloHello

Iterating Through String first types of string:

Using for loop we can iterate through a string. Here is an example to count the number of ‘l’
in a string

calculate frequency of 'l' in given string
str="Hello World"
cnt=0
for i in str:
if (i=='l'):
cnt+=1
print("frequency of l = ", cnt)
frequency of l = 3

find frequency of given character from types of string:

str=input("enter string")
print (str)
chr=input("enter character to be searched")
print(str)
cnt=0
for i in str:
if (i==chr):
cnt+=1
print("frequency of l = ", cnt)
enter string
work while you work and play while you
play
enter character to be searched
w
frequency of w = 4

So, display string content by using for loop:

str=input("enter string")
str_len =len(str)
print(str_len)
for i in range(0,str_len):
print(str[i])
enter string Pankaj
6
P
a
n
k
a
j

Then To display string in the same line (in python 3):

str=input("enter string")
str_len =len(str)
print(str_len)
for i in range(0,str_len):

//Then the
print(str[i],end=” “)
enter string pankaj
6
p a n k a j

So display string in the same line (in python 3)

for i in range(4): 
 print(a[i]),

3)Moreover String Membership Test:

We can test if a sub string exists within a string or not, using the keyword in.

>>> 'a' in 'program'
True
>>> 'at' not in 'battle'
False

To check whether the given character exists in a string or not by using Membership Test:

ans=0
str=input("enter string")
ch=input("enter character which is to be 
searched")
for i in str:
 if ch in str:
 ans=1
 break
if (ans==1):
 print("character found")
else:
 print("character not found")

/////////////////////////////////////////
enter string pankaj
enter character which is to be searched p
character found
enter string pankaj
enter character which is to be searched x
character not found

The enumerate()function

So, The enumerate()function returns an enumerate object. Then It contains the index and value of all the items in the string as pairs. So, This can be useful for iteration

str="viraj"
enum=enumerate(str)
print(enum)
enum=list(enumerate(str)) # [(0, 'v'), (1, 'i'), (2, 'r'), (3, 'a'), (4, 'j')]
print(enum)

To display list using enumerate:

list1=["red","pink","blue","orange"]
for ele in list1:
 print(ele)
//////////////////////
red
pink
blue
orange
/////////////////////////////////
list1=["red","pink","blue","orange"]
enum=enumerate(list1)
for ele in enum:
 print(ele)
////////////////////
(0, 'red')
(1, 'pink')
(2, 'blue')
(3, 'orange')
///////////////////////
list1=["red","pink","blue","orange"]
for ele in enumerate(list1,100):
 print(ele)
///////////////
(100, 'red')
(101, 'pink')
(102, 'blue')
(103, 'orange')
///////////////////
list1=["red","orange","pink","blue"]
enum = enumerate(list1,1)
for cnt,i in enum:
 if (cnt%2==0):
 print(i) 
/////////////////
Orange
Blue

The len() method returns the length of a string:

a = "Hello, World!"
print(len(a))
13
>>> a = " Hello, World! "
>>> print len(a)
15

The strip():

So, The strip() method removes any whitespace from the beginning or the end:

a = " Hello, World! "
print(a.strip())# returns "Hello, World!"
>>> print(len(a)) # at the time of display, it strips blank spaces but content of a string
15 will not be altered
>>> b = a.strip()
>>> print(len(b))
13

After that The lower() method returns the string in lower case:

a = "Hello, World!"
print(a.lower())
hello, world!

So The upper() method returns the string in upper case:

a = "Hello, World!"
print(a.upper())
HELLO, WORLD!
// Conversion from lowercase to uppercase
str=raw_input("Enter String")
for i in str:
 if (ord(i)>= 97) and (ord(i) <= 122):
 ch=chr(ord(i)-32)
 print(ch),
 else:
 print(i),

The replace() method replaces a string with another string:

a = "Hello, World!"
print(a.replace("H", "J"))
Jello, World!
//////////////////////
str="Pankaj Purvi Pragnesh"
print(str.replace("P","*"))
............................
*ankaj *urvi *ragnesh


Python Programming Archives – Learning Points

also for more studies visit our website- learning points – Home | A better learning future starts here

Leave a Reply

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