본문 바로가기

분류 전체보기

[Python] 문자열 바꾸기 - replace() 특정 문자열을 다른 단어로 바꿀수도 있어요. Hello, I’m Hello kiti.라는 문자을 Hi, I’m Hello Kiti.로 바꿔볼게요 introduce_str = "Hello, I'm Hello Kiti." print(introduce_str.replace('Hello', 'Hi') >> (출력) Hi, I'm Hi Kiti. replace() 함수를 이용해서 파라미터 안에 (어떤 문자를 바꿀 것인지, 어떻게 바꿀 것인지) 작성해주면 문자열을 바꿀 수 있어요. 그런데 원래 바꾸려고 했던 문장은 “Hi, I’m Hello Kiti.” 아니었나요? Hello라는 단어가 전부 바뀌었네요. 이럴 때는 아래처럼 해결할 수 있어요. introduce_str = "Hello, I'm Hello Kiti.".. 더보기
[Python] 공백 없애기 - strip(), lstrip(), rstrip() 이번에는 공백을 지워볼까요? hello_str = ‘ hello ’ 파이썬에서는 공백을 지우는 방법이 3가지나 있어요. 왼쪽에 있는 공백을 지우거나 오른쪽에 있는 공백을 지울 수 있고 양쪽 공백을 모두 지울수도 있어요. 그럼 소스코드를 통해 알아볼까요? hello_str = ' hello ' print(hello_str.lstrip()) print(hello_str.rstrip()) print(hello_str.strip()) >> (출력) hello *(여기까지 공백이 있습니다.)* >> (출력) hello >> (출력) hello strip()이라는 함수를 이용해 공백을 제거할 수 있는데 이해하기 쉽게 함수명 앞에 l이나 r을 붙여주면 왼쪽 공백을 지우거나 오른쪽 공백을 지울 수 있네요. (물론 ls.. 더보기
[Python] 대문자, 소문자 바꾸기 - upper(), lower() 이번에는 문자열을 모두 대문자 또는 소문자로 바꿔볼게요. hello라는 문자를 HELLO로 바꾸고, THANK YOU라는 문자를 thank you로 바꿔볼거에요. lower_str = 'hello' upper_str = 'THANK YOU' print(lower_str.upper()) print(upper_str.lower()) >> (출력) HELLO >> (출력) thank you 예시 소스코드처럼 문자열에 upper() 함수를 사용하면 모든 문자가 대문자로 작성이되요. 마찬가지로 lower() 함수를 사용하면 모든 문자가 소문자로 작성되고요. 그럼 문자열의 대소문자 변환은 언제 사용할까요? 당연히 문자열을 모두 대문자로 변환하거나 소문자로 변환하고 싶을 때 사용하겠죠? 특히 문자열을 비교할 때 자주.. 더보기
[Python] 문자 삽입 - join() 이번에는 문자 사이에 ‘,’를 넣어볼게요. 소스코드를 작성하기 전에 직접 만들어볼까요? abcdefg → a,b,c,d,e,f,g 이제 파이썬으로 구현해볼게요. alphabat_str = 'abcdefg' print(','.join(alphabat_str)) >> (출력) a,b,c,d,e,f,g 이렇게 join() 함수를 통해 문자들 사이의 ‘,’를 삽입할 수 있어요. 물론 점(.)이나 세미콜론(;), 콜론(:)과 같이 문자를 구분할 수 있는 다른 기호들도 삽입이 가능해요. 문자를 구분해줄 때 편리하게 사용할 수 있겠죠? 더보기
[Python] 특정 문자의 위치 찾기 - find(), rfind(), index() 이번에는 특정 문자가 몇 번째 위치에 있는지 찾아볼게요. 위에서 사용했던 “Hello World.”를 다시 사용해볼게요. 이 문장에서 ‘W’는 몇 번째에 있을까요? 공백(띄어쓰기)과 특수문자를 포함해주세요. 세어봤나요? 몇 번째 인가요? 문장의 7번째 글자가 ‘W’네요. 그럼 파이썬을 이용해서 W를 찾아볼까요? 먼저 파이썬에서 제공하는 find() 함수를 이용해볼게요. hello_str = 'Hello World.' print(hello_str.find('W')) >> (출력) 6 잉? 분명 손으로 세어봤을 때는 7번째 글자인데 왜 6이 나올까요? 여러분들은 제일 첫 글자인 H를 1번으로 세었을 거에요. 하지만 컴퓨터는 0번째, 1번째… 이렇게 세기때문에 ‘W’가 6번째에 있다고 알려주는 거에요. 그럼 .. 더보기
[Python] 문자 개수 세기 - count() 아래 문자에는 영어 단어 ‘l’이 몇 개 있을까요? “Hello World.” 다들 손가락으로 몇 개인지 세어보셨나요? 3개가 있죠? 파이썬의 문자열은 count() 라는 함수는 검색할 문자를 찾아 몇 개인지 계산해주는데요. hello_str = 'Hello World.' print(hello_str.count('l')) >> (출력) 3 hello_str2 = 'Hello World. Hello~' print(hello_str2.count('Hello')) >> (출력) 2 count() 함수에 검색할 문자나 단어를 파라미터로 입력해주면 문장 안에 몇 번 등장하는지 계산해줘요. 쉽죠? 더보기
[Codility] Lesson 3 : PermMissingElem - JAVA 문제 An array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing. Your goal is to find that missing element. Write a function: class Solution { public int solution(int[] A); } that, given an array A, returns the value of the missing element. For example, given array A such that: A[0] = 2 A[1] = 3 A[2.. 더보기
[Codility] Lesson 3 : FrogJmp - JAVA 문제 A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D. Count the minimal number of jumps that the small frog must perform to reach its target. Write a function: class Solution { public int solution(int X, int Y, int D); } that, given three i.. 더보기