Skip to main content

String Data Structure in Python

 String Data Structure in Python:


A string is a sequence of characters and is one of the most basic data structures in programming. In Python, strings are represented using quotes, either single quotes (') or double quotes ("). Strings are immutable, which means that once you create a string, you can't change its contents. However, you can create new strings from existing strings by concatenating or repeating them.

  • Concatenation: You can use the + operator to concatenate two or more strings and create a new string. For example:
python
>>> first_name = "John" >>> last_name = "Doe" >>> full_name = first_name + " " + last_name >>> print(full_name) John Doe
  • Repetition: You can use the * operator to repeat a string a specified number of times. For example:
python
>>> word = "Hello" >>> print(word * 3) HelloHelloHello
  • Slicing: You can extract a portion of a string by slicing it. The slice consists of all the characters from a start index to an end index, excluding the character at the end index. For example:
python
>>> word = "Hello" >>> print(word[1:4]) ell
  • Length: You can use the len function to find the number of characters in a string. For

  • example:
  • python
    >>> word = "Hello" >>> print(len(word)) 5
    • Membership: You can use the in operator to test if a character or a substring is present in a string. For example:
    python
    >>> word = "Hello" >>> print('H' in word) True >>> print('h' in word) False
    • Comparison: You can compare two strings to see if they're equal, less than, or greater than each other. For example:
    python
    >>> word1 = "Hello" >>> word2 = "hello" >>> print(word1 == word2) False >>> print(word1 < word2) False >>> print(word1 > word2) True
    • Formatting: You can use the format method to insert values into a string. For example:

    • python
      >>> name = "John" >>> message = "Hello, {}".format(name) >>> print(message) Hello, Joh

a number of built-in methods for string manipulation and processing. Here are some of the most commonly used ones:

  • upper: Converts all the characters in a string to uppercase. For example:
python
>>> word = "Hello" >>> print(word.upper()) HELLO
  • lower: Converts all the characters in a string to lowercase. For example:
python
>>> word = "Hello" >>> print(word.lower()) hello
  • strip: Removes whitespaces from the beginning and end of a string. For example:
python
>>> word = " Hello " >>> print(word.strip()) Hello

  • replace: Replaces all occurrences of a substring with another string. For example:
python
>>> word = "Hello, World!" >>> print(word.replace("World", "Python")) Hello, Python!

  • split: Splits a string into a list of substrings based on a specified separator. For example:
python
>>> word = "Hello, World!" >>> print(word.split(",")) ['Hello', ' World!']


  • join: Joins a list of strings into a single string, using a specified separator. For example:
python
>>> words = ["Hello", "World"] >>> print(", ".join(words)) Hello, World



These are just a few of the many string operations and methods available in Python. By combining these operations and methods, you can perform a wide range of string processing tasks in an elegant and efficient manner.




By itsbilyat

Comments

Popular posts from this blog

Limitations of python

 Limitations of python While Python is a powerful and flexible programming language, it does have some limitations that should be considered when deciding whether to use it for a particular project. Here are some of the limitations of Python: Performance: Python is an interpreted language, which means that the code is executed line by line, rather than being compiled into machine code before execution. This can make Python programs run slower than programs written in compiled languages like C++ or Java. For performance-critical applications, Python may not be the best choice. Memory usage: Python uses dynamic typing, which means that the type of data stored in a variable can change dynamically during the runtime of a program. This can result in higher memory usage compared to statically typed languages like C++ or Java. Lack of low-level control: Python is a high-level language that provides a high level of abstraction. This makes it easy to write code quickly, but it can also limi...

TUPLE DATA TYPE IN PYTHON

 TUPLE DATA TYPE IN PYTHON: A tuple is an ordered, immutable collection of elements in Python. Tuples are often used to store multiple related pieces of information in a single structure. Here are some key points about tuples in Python: Syntax: Tuples are defined by enclosing a comma-separated list of elements within parentheses. For example: (1, 2, 3, 4). Immutable: Once a tuple is created, its elements cannot be changed. This makes tuples ideal for storing data that should not be modified. Indexing: Tuples can be indexed just like lists, with the first element having an index of 0. For example: t = (1, 2, 3); t[1] would return 2. Slicing: Tuples can be sliced just like lists, using the square bracket syntax. For example: t = (1, 2, 3); t[0:2] would return (1, 2). Nesting: Tuples can contain elements of any data type, including other tuples. For example: t = ((1, 2), (3, 4)); t[0] would return (1, 2). Unpacking: Tuples can be unpacked into individual variables. For example: t = (1...

Continue statement in Python

Continue statement in Python   The "continue" statement in Python is used within a loop to skip the rest of the current iteration and move on to the next one. This statement can be useful in cases where you want to skip a certain condition or value during the iteration, but still want to continue processing the rest of the elements. Here is an example to illustrate the use of the "continue" statement in a for loop: python Copy code for i in range ( 10 ): if i % 2 == 0 : continue print (i) In this example, the "continue" statement is used to skip the processing of all even numbers. The loop iterates over the range from 0 to 9, and for each iteration, it checks if the current number i is divisible by 2. If it is, the "continue" statement is executed and the rest of the iteration is skipped. If i is not divisible by 2, the current number is printed. The output of this code will be: Copy code 1 3 5 7 9 As you can see, all even ...