Strings in Python

Strings in Python

What are Strings in Python?

The string data type is used to represent textual data in python. A String in python is a sequence of characters, they need to be enclosed within either 'single quotes' or "double quotes" characters.

Example:

name='iron man'
user_name='ironman@avengers.com'

Example Single Line String:

print("Hello")
#Hello
print('Hello')
#Hello

Example Multi-Line String:

a = """We can store strings like this,
These are called multiline strings,
can even continue some x number of lines."""
print(a)

Finding length of String in Python

There is a built-in (len) function that returns the length of the string -which means the number of characters in the string.

len('dogs') 
# 4
len('cute dogs')
# 9
# since the space is also character b/w them so 9.

String indexing in Python

String indexing is used to grab a single character from a string. The first character is accessed with the [index 0]the second using the [index 1] and so on...

Example:  

str='cute dogs'
# c - 0
# u - 1
# t - 2
# e - 3
#   - 4
# d - 5
# o - 6
# g - 7
# s - 8

Example:

str='cute dogs'
str[5] 
# d
# since space ia a character will be 'd'

Example:

my_str='all dogs are cute'
print(my_str[8])                           
#s

String negative indexing in Python:

Python strings also support negative indexing which means you can access the last character of a string using the index[-1], the second last character using the index[-2], and soo on.

Example:  

str='cute dogs'
# c - -9
# u - -8
# t - -7
# e - -6
#   - -5
# d - -4
# o - -3
# g - -2
# s - -1

Example:

str='cute dogs'
str[-6] 
# e

Note: Accessing a character beyond the length of the string will lead to an indexError in Python. Example: for above string str[1000] and str[-100] # throws an Error.

String slicing in Python:

String slicing is used to grab a slice of the string. Imagine you want to extract the string 'cute' from the string 'cute dogs' you can use slicing like:

Example:

{syntax:str[index[i]:index[j+1]}
str='cute dogs'
new_str=str[0:4] 
#cute
new_str=str[5:] 
#dogs
new_str=str[:4] 
#cute
new_str=str[::] 
#cute dogs
#Gets entire string
new_str=str[::-1]
#sgod etuc
#Reverse the string

Example:

str='cute dogs'        
#hint: starting at index -9 pick every 2nd character up to but not including index -1
print(str[-9:-1:2])     
# ct os 
#we need to pick every 2nd character up to but not including index-1(which is last character).

Example:

print(str[-9::3])       
#ceo (starting at index -9 which brings us to the start of the string we need to pick every 3rd character.

String concatenation in Python

The string can be appended to each other using the '+'operator.

Example:

str1='cute'
str2='dogs'
str3=str1+' '+str2
print(str3)                 
#cute dogs

String repitation in Python

The string can be repeated by using the multiplication or '*' operator.

Example:

str='wow'
new_str=str*3        
#wow wow wow 

Did you find this article valuable?

Support Prahlad Inala by becoming a sponsor. Any amount is appreciated!