How to use strings ( slicing, concatenate ,formatting) in python syntax | for beginners
#python #strings #pythonsyntax
Creating a String
To create a string in Python, you can simply enclose a sequence of characters in quotes. For example:
```python
my_string = “Hello, World!”
```
You can also use single quotes to create a string:
``python
my_string = ‘Hello, World!’
```
Both single and double quotes can be used interchangeably, as long as they are used consistently within the same string.
Accessing Characters in a String
You can access individual characters in a string by using indexing. In Python, indexing starts from 0, so the first character of a string is at index 0, the second character is at index 1, and so on. For example:
```python
my_string = “Hello, World!”
print(my_string[0]) # Output: H
print(my_string[7]) # Output: W
```
You can also use negative indexing to access characters from the end of the string. For example, `-1` represents the last character, `-2` represents the second last character, and so on.
1 view
121
16
9 months ago 00:21:40 1
Ի՞նչ են զգուշացրել ԱՄՆ պաշտոնյաները Էրդողանին Զանգեզուրի հարցով. Նիկոլի հեռացումը օրակարգում է
9 months ago 00:05:49 1
Falling In Reverse - “Losing My Life“
9 months ago 00:43:25 1
[ENG](코바늘가방)트윈퍼프핸들백How to crochet A twin puff handle bag