String Handling in Python, What is a String?, declaration of strings in Python, concatenating strings, and string comparison in Python.
String Handling in Python
What is String?
The string is a sequence of characters written in single quotes or in double quotes or in three double-quotes.
Examples:
string1= ‘This is single quoted string’;
string2= “This is double quoted string”;
string3= “””This is triple quoted string”””;
print (string1);
print (string2);
print (string3);
————————–
String may have Alphabets, Numbers, and Special Characters
Examples:
a=”India”;
b= “100”;
c =”India123″;
d =”*&%$”;
e = “123*&%$”;
f =”India123*&%$”;
g=”Selenium With Java”
print (a);
print (b);
print (c);
print (d);
print (e);
print (f);
print (g);
————————–
How to use quotation marks in Strings?
Examples:
string1 =’Hello Python Don’t’;
string2 =”Python” Strings”;
print (string1);
print (string2);
use Forward Slash (/) to avoid Syntax errors…
string1 =’Hello Python Don\’t’;
string2 =”Python\” Strings”;
print (string1);
print (string2);
Important Operations on Strings…
1. Finding String length
string1 =”Hello Python”;
print (len(string1));
2. Concatenating Strings
string1 =”Hello “;
string2=”Python”;
print (string1 + string2);
————————–
string1 =”Hello “;
num=100;
print (string1 + str(num));
3. Print a String multiple times
string1 =”Hello “;
print (string1 *10);
4. Check whether the String having all numeric characters?
string1 =”Hello”;
print (string1.isnumeric()); # False
string2 =”Hello123″;
print (string2.isnumeric()); # False
string3 =”12345″;
print (string3.isnumeric()); # True
5. Check whether the String having all alphabetic characters?
string1 =”Hello”;
print (string1.isalpha()); # True
string2 =”Hello123″;
print (string2.isalpha()); # False
string3 =”12345″;
print (string3.isalpha()); # False
6. Capitalize a String
str1 = “python programming language”
str2=”PYTHON PROGRAMMING”
str3= “python1234″
str4=”12345″
str5=”&*(”
x= str1.capitalize()
print(x) #Python programming language
print(str2.capitalize()) #Python programming language
print(str3.capitalize())
print(str4.capitalize())
print(str5.capitalize())
7. Convert a string into lower case
str1 = “python programming language”
str2=”PYTHON PROGRAMMING”
str3= “PYTHON1234″
str4=”12345″
str5=”&*(”
print (str1.lower()) #python programming language
print (str2.lower()) #python programming
print (str3.lower()) #python1234
print (str4.lower()) #12345
print (str5.lower()) #&*(
8. Check whether a string ends with a specified suffix or not?
str1 = “Python Programming Language”
print (str1.endswith(“e”)) #True
print (str1.endswith(“age”)) #True
print (str1.endswith(“Language”)) #True
print (str1.endswith(“Programming Language”)) #True
print (str1.endswith(“Programming”)) #False
9. count() method
str1 = “Python is a Programming Language, Python is an Open source software and Python”
print (str1.count(“is a”)) # 2
10. replace() method
str1 = “Python is a Programming Language, Python is an Open source software, and Python”
print (str1) # Python is a Programming Language, Python is an Open source software, and Python
print (str1.replace(“Python”, “JavaScript”)) # JavaScript is a Programming Language, JavaScript is an Open source software, and JavaScript
Python Step by Step Tutorial
1. Introduction to Python Programming Language
2. Download and Install Python
Python Environment Setup (Using PyCharm IDE)
4. Python Keywords and Identifiers
9. Python Conditional Statements
11. Python Branching Statements
14. Python Data Structures – Lists
15. Python Data Structures – Sets
16. Python Data Structures – Tuples
17. Python Data Structures – Dictionaries
18. Python User Defined Functions
25. Python Regular Expressions
26. Python Object-Oriented Programming
29. Abstraction and Encapsulation in Python