Everything about Python Strings

What are Strings?

Strings: Strings are sequences of characters enclosed by single(‘ ’) or double quotes (“ ”). This sequence of Unicode characters may include letter, number and a special character.

How to create a String?

A string can be created by simply enclosing characters in single or double quotes.

str="Eshaa"
print(str)

Empty Strings

An empty string is a string without any characters inside,having length zero.

str="  "
print(str)

Multiline Strings

Multiline strings are represented using triple quotes(''' ''').

str='''Have a Nice Day'''
print(str)

Traversing a String

Traversing a string means accessing all the elements of a string one after the other by using index value.

Each character in a string has an index value. Indexing starts at 0 and must be a integer.

Individual elements can be accessed by typing index values inside square brackets

[ ] as shown below in the code.

str="HELLO"
print(str[0])
print(str[1])
print(str[2])
print(str[3])
print(str[4])

Iterating through the string using loop

str="Hello World"
for i in str:
    print(i)

Special String Operations:

1.Concatenation: Concatenation means adding of 2 strings. The + operator joins two strings.

str1="Hello"
str2="World"
str3=str1+str2
print(str3)

2.Replication: The * operator creates a new string by repeating multiple copies of the same string.

str="Eshaa"
str*3

3.String Slicing: Slicing is used to retrieve a subset of values. This extracted substring is known as slice.

Syntax: String_name[Start:End:Step]

str="Computer"
print(str[1:3])
print(str[:3])
print(str[3:])
print(str[::2]

STRINGS ARE IMMUTABLE

Strings are immutable which means that the content of the string cannit be changed after it is created.

str="can"
str[0]='m'

The above code will lead to Type ERROR since Python does not allow the programmer to change a character in the string.

String Built in Functions

**1. len():**This method returns the length of the string.

str="Hello World"
print(len(str))

2. capitalize(): This method returns the exact copy of string with first letter in uppercase.

str="eshaa"
print(str.capitalize())

3. replace(): This function replaces all the occurrences of old string with new string.

str="Today is a good day"
print(str.replace('is','was'))

4. split(): The split() method breaks up a string at the specified separtaor and returns a list of substring.

str="I am Eshaa"
print(str.split())

5. find(): This function is used to search the first occurrence of substring in a given string. The find method returns the lowest index of the substring. If the substring is not found in the given substring it returns -1.

str="Computer"
print(str.find('put'))

6. isalpha(): This function check whether the inputted string contains alphabets.It returns True if the string contains only letters,otherwise returns False.

str="Good Day"
print(str.isalpha())

The output of this code will be False since the string contains spaces.

7. upper(): Converts the lowercase letters in the string to uppercase.

str="bye"
print(str.upper())

8. lower(): Converts the uppercase letters in the string to lowercase.

str="BYE"
print(str.lower())

9. lstrip(): Returns the string after removing the spaces from the left of the string.

str="  Hello"
print(str.lstrip())

10. rstrip(): Returns the string after removing the spaces from the right of the string.

str="Hello   "
print(str.rstrip())

11. swapcase(): This method converts and return all the uppercase character into lowercase or vice versa of the given string.

s="Hello"
print(s.swapcase())

Thanks for reading! 😻

Feel free to connect with me on Twitter and Linkedin. 😃

Did you find this article valuable?

Support Eshaa Bhasin by becoming a sponsor. Any amount is appreciated!