2.1.2
Strings
Strings
Strings
Strings are a special case of an array or list, where each element is a character. There are many methods we can apply to them:
![Illustrative background for Concatenation](https://image-v2.cdn.app.senecalearning.com/2018-08/c7c2a77c-8f38-450a-a083-bbfd3f6801d4/plus-sign-add-,h_400,q_80,w_640.png)
![Illustrative background for Concatenation ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/c7c2a77c-8f38-450a-a083-bbfd3f6801d4/plus-sign-add-,h_400,q_80,w_640.png)
Concatenation
Concatenation
- Concatenation is joining two strings together.
- This can be done with the + sign. E.g:
- "Hello " + "World" = "Hello World".
![Illustrative background for Accessing a character](https://image-v2.cdn.app.senecalearning.com/2018-09/8825925c-709f-415f-b053-5473ac469099/positive-letters-2355685_1920,h_400,q_80,w_640.jpg)
![Illustrative background for Accessing a character ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-09/8825925c-709f-415f-b053-5473ac469099/positive-letters-2355685_1920,h_400,q_80,w_640.jpg)
Accessing a character
Accessing a character
- Remember, strings are just lists. So we can access a specific character in the exact same way as we access a list element:
- string[3] would get the fourth character.
![Illustrative background for Length of a string](https://image-v2.cdn.app.senecalearning.com/2018-05/d02fc304-19d2-4611-a75e-08d788b5e2cd/letters-656514_960_720,h_400,q_80,w_640.png)
![Illustrative background for Length of a string ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-05/d02fc304-19d2-4611-a75e-08d788b5e2cd/letters-656514_960_720,h_400,q_80,w_640.png)
Length of a string
Length of a string
- The length of a string is given by the len function:
- len("Hello") would give 5.
![Illustrative background for Substrings](https://image-v2.cdn.app.senecalearning.com/2018-07/e12c850d-b9d5-4d02-b2a2-748988ccce22/Puppet-Control-String-Wood,h_400,q_80,w_640.jpg)
![Illustrative background for Substrings ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-07/e12c850d-b9d5-4d02-b2a2-748988ccce22/Puppet-Control-String-Wood,h_400,q_80,w_640.jpg)
Substrings
Substrings
- A substring is a part of a longer string.
- In Python we take a substring using:
- stringname[startPos:endPos]
- This takes a substring starting at the 'startPos'th character, and finishing at the 'endPos'th character.
1Introduction to Python
1.1Python
1.2Input & Output
1.3Data Types
1.4Variables & Constants
1.6Program Flow
2Data Structures
2.1Lists & Strings
2.2File Handling
3Modularity
3.1Subroutines
3.2Data Flow
4Good Practice
4.1Naming & Whitespace
4.2Comments
4.3Exceptions
Jump to other topics
1Introduction to Python
1.1Python
1.2Input & Output
1.3Data Types
1.4Variables & Constants
1.6Program Flow
2Data Structures
2.1Lists & Strings
2.2File Handling
3Modularity
3.1Subroutines
3.2Data Flow
4Good Practice
4.1Naming & Whitespace
4.2Comments
4.3Exceptions
![Go student ad image](/en-GB/revision-notes/_next/image?url=%2Fen-GB%2Frevision-notes%2Fimages%2Fgo-student-uk-ad.jpg&w=640&q=100)
Unlock your full potential with GoStudent tutoring
Affordable 1:1 tutoring from the comfort of your home
Tutors are matched to your specific learning needs
30+ school subjects covered