String: Python

Share via:

String:

It is one of the built-in data structures in python.
String is a group of one or more characters which are enclosed in between single, double, triple quotes.
Strings can contain symbols, letters, numbers and special characters.
Strings are immutable i.e. once they are created, they can’t be changed but you can create new strings based on the original strings.
Strings supports many operations like manipulation, formatting, searching and slicing.

 

String creation.

You can create a string by enclosing a group of characters within single quotes or double quotes.
When you want to use the single quote in a string then enclose it with double quotes which is shown in below example.
You can check the datatype of the data structure that you created using type( ) function.
Examples:

Accessing elements of strings

The elements in the strings are accessed using the indexes.
Indexes start with zero and end with a number which is one less than the length of the string.
Python also supports the negative indexing which start from last element with index -1 to – length of the string.
Consider the string s=”HELLOWORLD”

 

 

  H   E   L   L   O   W   O   R   L   D
  0   1   2   3   4   5   6   7   8   9
  -10   -9   -8   -7   -6   -5   -4   -3   -2   -1
By looking at the table shown above
To access the first element, you have to use the index 0 and the last element with  number one less than less than the string length i.e. 9
Example:

 

Slicing in strings

Slicing is taking a part of the original string i.e. a substring.
This is done by using the slice operator in python.
Syntax: string_name[start_index : End_index]
start_index is included and end_index is excluded in the substring you extract
Example:

 

Slicing to reverse the string.

You can reverse the string using the slice operator.
Syntax: string_name[ :: -1]
Example:

 

Deletion of string

Once the string gets created in python you can’t delete or modify the characters in the string, when you try to modify the string you will get the error.
But you can delete the whole string using the del keyword.
String Methods
1. Capitalize (): converts the first letter of the string to uppercase and the rest of the string to lowercase.
Example

2. Count (): used to count the number of occurrences of a specified substring within a string.
Example

3. find (): returns the lowest index of substring in the given string if the substring is not found return -1.
Example

4. index (): returns the lowest index of substring in the given string if the substring is not found return Value Error.
Example

5. isalnum ( ): returns True if all the characters in the strings are alphabetic letters or numbers otherwise False.
Example

6. isalpha (): returns True if all the characters in the strings are alphabetic letters otherwise False.
Example

7. isdigit (): returns True if all the characters in the strings are digits otherwise False.
Example

8. islower ( ): returns True if all the characters in the strings are lowercase otherwise False.
Example

9. isupper ( ): returns True if all the characters in the strings are uppercase otherwise False.
Example

10. join ( ): used to concatenate elements of an iterable like list tuple etc into a single string.
This method takes an iterable as an argument and returns a string.
Example

11. title ( ): converts the given string title case i.e. the first character of each word is converted to uppercase.
Example

12. lower ( ): converts all the characters in the given string to lowercase
Example

13. upper (): converts all the characters in the given string to upper case.
Example

14. replace ( ): replace occurrences of a specified substring within a string with another substring.
This method takes three parameters: old_substring, new_substring,count. count is
Example

 

Author    : Venkat Vinod Kumar Siram 

LinkedIn : https://www.linkedin.com/in/vinodsiram/

Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates.

KTExperts is always active on social media platforms.

Facebook  : https://www.facebook.com/ktexperts

LinkedIn    : https://www.linkedin.com/company/ktexperts/

Twitter       : https://twitter.com/ktexpertsadmin

YouTube   :  https://www.youtube.com/c/ktexperts

Instagram  : https://www.instagram.com/knowledgesharingplatform

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Add Comment