본문 바로가기
Python(Eng. ver)

3. Python String & why 0.1+ 1.1 ≠ 1.2

by 곽정우 2024. 3. 13.

1. What are Strings?

In Python, strings are a crucial data type used to handle text data. They are represented by sequences of characters enclosed in single (') or double quotes ("), or triple quotes (''' or """) for multi-line strings.

 

2. String Reassignment

Since strings in Python are immutable, reassigning a string to the same variable creates a new string object, and the variable references the new object. The previous string object remains unchanged.

 

3. String Functions and Operators

 

3-1. Checking String Length:

  • len() function : It returns the length of a string.

 

3-2. String Concatenation:

  • (+): It combines two strings to create a new string.

 

3-3. String Repetition:

  • (*): It repeats a string a specified number of times.

 

3-4. String Indexing and Slicing:

  • Strings can be accessed and sliced using indexes for individual characters.

 

Indexing:

  • Each character has an index starting from 0.
  • The [] operator can be used to access specific characters by their indexes.

인덱싱 결과

 

Slicing:

  • The [start:end] syntax extracts a portion of the string.
  • The Start indicates the starting index, and the End indicates the ending index.
  • Omitting End extracts until the end of the string.

슬라이싱 결과

 

4. String Methods

String methods are functions applied to string objects, allowing various manipulations and transformations.

 

4-1. upper()와 lower() - Case Conversion

  • upper(): Converts all characters in a string to uppercase.
  • lower(): Converts all characters in a string to lowercase.

 

4-2. count() - Counting Occurrences of a Substring

  • count(sub_str): Returns the number of occurrences of a specific substring (sub_str) in a string.

 

4-3. find() - Finding the First Occurrence of a Substring

  • find(sub_str): Returns the index of the first occurrence of a substring (sub_str) in a string.
  • find(sub_str, start_idx): Allows specifying the starting index for the search.

 

4-4. replace() - String Replacement

  • replace(old_str, new_str): Replaces all occurrences of a substring (old_str) with another substring (new_str) in a string.

 

4-5. strip() - Removing Whitespace

  • strip(): Removes whitespace from both sides of a string.
  • lstrip(): Removes whitespace only from the left side of a string.
  • rstrip(): Removes whitespace only from the right side of a string.

 

4-6. split() - Splitting a String

  • split(sep): Separate the string based on the specified separator (sep) and return it to the list.

 

4-7. join - Joining Strings

  • join(list): Joins the elements of a list with a specified delimiter (sep) and returns a string.

 

4-8. startswith()와 endswith() - Checking Prefixes and Suffixes

  • startswith(prefix): Checks if a string starts with a specific prefix.
  • endswith(suffix): Checks if a string ends with a specific suffix.

 

※. Why 0.1 + 1.1 ≠ 1.2

 

1. Floating-Point Representation:

Computers use floating-point representation to store real numbers, consisting of an exponent and a significand. This represents the number's magnitude and precision.

 

2. Cause of 0.1 + 1.1 ≠ 1.2:

0.1 and 1.1 can be expressed accurately in decimal, but since they are infinite decimals in binary, they cannot be expressed accurately in floating point representation. Because the computer stores these numbers as approximate values, the actual calculation result will differ from 1.2.

 

3. Dealing with Floating-Point Errors:

  • Using the Decimal library: Python's Decimal library allows for calculations without floating-point errors.
  • Using rounding functions: Rounding functions like round() can be used to obtain results with a desired number of decimal places.
  • Using the fsum() function: The fsum() function helps prevent error accumulation by summing numbers in a list sequentially.

4. Conclusion:

0.1 + 1.1 ≠ 1.2 is a phenomenon caused by the limitations of floating-point representation. Floating-point errors can impact various areas, so it's crucial to be aware of the potential for errors and apply appropriate solutions.

 

5. References:

 

 

 

 

 

'Python(Eng. ver)' 카테고리의 다른 글

6. Python collection type - Set  (0) 2024.03.14
5. Python collection type - Tuple  (0) 2024.03.14
4. Python collection type - List  (0) 2024.03.14
2. Variables in Python  (0) 2024.03.13
1. Python Output  (0) 2024.03.12