Strings in Python:
Like many other programming languages, strings in python is also an array of bytes, that represents Unicode characters.
However, Python doesn't have a character (char) data type. A single character is simply a string with a length of 1.
In python, we use square brackets to access the elements of strings. We can use both single and double quotes in python to define a string.
Print("strings in double-quotes") #this will print -> strings in double-quotes
Print('strings in single quotes') #this will print -> strings in single quotes
Print("strings in double-quotes") #this will print -> strings in double-quotes
Print('strings in single quotes') #this will print -> strings in single quotes
Assigning String to a variable:
Let's take an example:str="hello world"
str1='Hey! This is a string example'
print(str)
print(str1)
Access elements in the string:
For accessing elements in strings, we use square brackets to access the elements.
str="Hello programmers" #in strings in python indexing start from 0print(str[2]) #here output will be "I"
Multiline Strings:
In Python, you can also use three quotes for writing multiline strings.
str=''' Hello world!This is Multiline stringsin python'''print(str)
Slicing in Python:
In Python Strings, You can return specific characters by using slice syntax.
Syntax:
a[start:end:gap]
a="Hello Programmers!"print(a[2:9:2]) #starts wih index 2 and ends at 9 with skip 2 .
Negative Indexing:
We can use negative indexing to access elements from back.
a = "Hello, World!"
print(a[-5:-2])
String Length:
To get the length of a string, use the
len()
function.a = "Hello, World!"
print(len(a))
0 Comments
Doubts? Please let our team know So that we can serve you better.